Why is my ASP ASP.NET MVC cookie not working?

I am using ASP.NET MVC 3, with form validation (based on the modified vanilla account code that you get with file-> new).

When you log in, I set a cookie with

FormsAuthentication.SetAuthCookie(userName, true); 

So this should set a persistent cookie. But if I close the browser and open again when I go to the site, I have to log in again! I can see, using the chrome dev tools, that a cookie (.ASPXAUTH) is created and not deleted when the browser closes, and what happens?

My web.config:

 <authentication mode="Forms"> <forms loginUrl="~/Account/LogIn" timeout="10000"/> </authentication> 

I am testing this locally, under IIS, if that matters.

+15
cookies asp.net-mvc forms-authentication
Jan 13 2018-12-12T00:
source share
2 answers

Solved from @alexl's comment:

you can check this answer: Permanent user registration with ASP.Net membership

Ok, this link seems to have sorted it for me - sticking with SetAuthCookie and tweaking my configuration to explicitly specify the cookie name (in the web.confg file) and now everything works. Weird! -

+3
Jan 17 '13 at 12:29
source share

I would rather create a cookie using an authentication ticket. SetAuthCookie creates a backup ticket under the hood. Have you tried making your own authorization ticket? It will allow you to store additional data on it .

Here is an example:

 // create encryption cookie FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddDays(90), createPersistentCookie, string.Empty); // add cookie to response stream string encryptedTicket = FormsAuthentication.Encrypt(authTicket); System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); if (authTicket.IsPersistent) { authCookie.Expires = authTicket.Expiration; } System.Web.HttpContext.Current.Response.Cookies.Add(authCookie); 

Hope this helps.

+8
Jan 13 '12 at 14:45
source share



All Articles