FormsAuthenticationTicket: how to keep the user logged in after the browser has been closed?
See the question and my original answer on StackOverflowFirst, you need to set the 5th parameter of the FormsAuthenticationTicket
constructor 'isPersistent
' to true.
Then I would add change the code to this:
var faCookie = new HttpCookie("CookieFA", FormsAuthentication.Encrypt(authTicket));
if (authTicket.IsPersistent)
{
faCookie.Expires = authTicket.Expiration;
}
Response.Cookies.Add(faCookie);
And if you also want to honor what's configured in web.config, you can add this extra code (optional):
var faCookie= new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
faCookie.Path = FormsAuthentication.FormsCookiePath;
if (FormsAuthentication.RequireSSL)
{
faCookie.Secure = true;
}
if (FormsAuthentication.CookieDomain != null)
{
faCookie.Domain = FormsAuthentication.CookieDomain;
}
...