ASP.Net Membership Forms Remember Me is not retained when hosted on Shared hosting

bahare
 
on Jan 17, 2013 08:45 AM
4757 Views

Hi,
why remember me doesn't work?
as you can see in picture below Expire date is at the end of session although I set expiration to 1 month. why?

this is my login button codes

 

protected void btn_login_Click(object sender, EventArgs e)
    {
        if (!Page.User.Identity.IsAuthenticated)
        {
        if (GetMemberAuthenticate(txt_uname.Text, FormsAuthentication.HashPasswordForStoringInConfigFile(txt_pass.Text, "MD5")))
        {
            string role = GetMemberRoleByID(txt_uname.Text);
            DateTime expiration;
            if (chk_rememberme.Checked)
            {
                expiration = DateTime.Now.AddMonths(1);
            }
            else
            {
                expiration = DateTime.Now.AddMinutes(10);
            }
            FormsAuthenticationTicket AuthTicket = new FormsAuthenticationTicket(1, txt_uname.Text, DateTime.Now, expiration, false, role);
            string encryptedTicket = FormsAuthentication.Encrypt(AuthTicket);
                HttpCookie AuthCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                Response.Cookies.Add(AuthCookie);
                string[] temp = role.Split(',');
                for (int i = 0; i < temp.Length; i++)
                {
                    if (temp[i] == "Admin")
                    {
                        Response.Redirect("AdminPage.aspx");
                    }
                }
                if (Session["CurrentUrl"] != null)
                {
                    Response.Redirect(Session["CurrentUrl"].ToString());
                }
                Response.Redirect(FormsAuthentication.GetRedirectUrl(txt_uname.Text, chk_rememberme.Checked));
            }
            else
            {
                lbl_login.Visible = true;
            }
        }
        else
        {
            lbl_login.Visible = true;
            lbl_login.Text = "";
        }
    }

and this is in global.asax

 

 

 

and in web.config

 

<authentication mode="Forms">
			<forms loginUrl="Default.aspx" defaultUrl="~/" name="MyCookieName"
slidingExpiration="true"
timeout="43200"></forms>
		</authentication>

 

void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.




        Session.RemoveAll();
        Session.Abandon();
        Application.Lock();
        Application["Online"] = (int)Application["Online"] - 1;
        Application.UnLock();

    }

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName.ToString()];
        if (authCookie != null)
        {
            FormsAuthenticationTicket MyTicket;
            MyTicket = FormsAuthentication.Decrypt(authCookie.Value);
            FormsIdentity MyIdentity = new FormsIdentity(MyTicket);
            GenericPrincipal MyPrincipal = new GenericPrincipal(MyIdentity, MyTicket.UserData.Split(new char[] { ',' }));
            Context.User = MyPrincipal;
        }

    }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
bahare
 
on Jan 31, 2013 12:33 AM

Hi,

I set machineKey in web.config and the problem solved.

hope this help somone else

cheers

Mudassar
 
on Jan 31, 2013 01:05 AM

If this is on a Shared hosting server then yes. You need set Machine Key and that's the cause of Remember Me not working