Advertise with us

“Remember me” functionality in a Login form

I am using linq to entity connection. I want to keep user logged in once he entered into his account, This is my code. It's not working. Help, please

    if (this.ChkRememberme != null && this.ChkRememberme.Checked == true)
    {
        HttpCookie cookie = new HttpCookie(TxtUserName.Text, TxtPassword.Text);
        cookie.Expires.AddYears(1);
        Response.Cookies.Add(cookie);
    }
Last updated:12/16/2013 12:52:12 AM

1 Answers

Pravesh Singh
Pravesh Singh

Hi Takeshi,


Try this :

if (this.ChkRememberme != null && this.ChkRememberme.Checked == true)
    {
 int timeout = rememberMe ? 525600 : 30; // Timeout in minutes, 525600 = 365 days.
    var ticket = new FormsAuthenticationTicket(TxtUserName.Text, TxtPassword.Text);
    string encrypted = FormsAuthentication.Encrypt(ticket);
    var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
    cookie.Expires = System.DateTime.Now.AddMinutes(timeout);
    cookie.HttpOnly = true; // cookie not available in javascript.
    Response.Cookies.Add(cookie);
}

Answer