ASP-MVC Forms Authentication - cookie not persisting
I have my own linq to sql database with a nice login method that returns the user to me.
I followed 101 examples online on how to add a cookie to a client.
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
_u.id.ToString(),
DateTime.Now,
DateTime.Now.AddDays(14),
true,
"hi",
FormsAuthentication.FormsCookiePath);
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
//Response.Cookies.Add(cookie);
//FormsAuthentication.RedirectFromLoginPage(_u.name, _remember);
FormsAuthentication.SetAuthCookie(_u.name, _remember);
And, of course, it will be added. But when I check it, the session expires, not two weeks as stated. Therefore, when a user tries to return to the site after closing the browser, he must be logged in.
Any ideas?
+1
source to share
2 answers
I have the same problem and I solve on login Page_Load
First confirm User.Identity is correct
if true we have a valid user !!!.
if false delete old cookie (See link http://forums.asp.net/t/1227365.aspx/1 )
This last part is to prevent the new cookie from being saved correctly.
-1
source to share