I have an MVC 4 website where a user can login and I save a cookie with their session information so they don\'t have to login again.
public void SetCookie(H
I think you answered your own question with persistent cookie. Regular cookies expire when the browser session ends (this is typically closing the browser). Persistent cookies have a date when they should be removed and can live across browser sessions.
Your code should look something like this:
private HttpCookie CreateAuthenticationCookie(int loginId, string username)
{
string userData = string.Format("loginId:{0},username:{1}", loginId, username);
var ticket = new FormsAuthenticationTicket(loginId, username,
DateTime.Now, DateTime.Now.AddYears(1),
false, userData, FormsAuthentication.FormsCookiePath);
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
return new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket){{
Expires = DateTime.Now.AddYears(1);
}};
}
This way your cookie will persist across browser sessions for one year.