FormsAuthenticationTicket, IsPersistent and HttpCookie.Expires - Thea Burger's Blog

Thea Burger's Blog

Wouldn't you like to know...

News

Photo's!!!

About me

I'm Reading: General Blogs

I'm Reading: Technical Blogs

FormsAuthenticationTicket, IsPersistent and HttpCookie.Expires

I'm using Forms Authentication and Role based security for an app I'm working on. Ran into a problem that even though I set the IsPersistent parameter of the FormsAuthenticationTicket to true, next time I open the app I have to log in again...

authTkt = new FormsAuthenticationTicket(1, userId, DateTime.Now, DateTime.Now.AddYears(1), true, userRoles);

Well, doh, the plain and simple answer is that you need to set the expiry date of the cookie, otherwise it expires when the user closes the browser. Problem solved :-)

authCookie.Expires = authTkt.Expiration;

More info on ASP.Net cookies on CodeProject.

Comments

Mike said:

Thank you, thank you, thank you!! This solved a problem I've been struggling with for weeks. Keep up the good work.
# June 7, 2005 6:34 PM