VenkatRJV VenkatRJV - 2 months ago 16
ASP.NET (C#) Question

Cookie expiring date not working C#

I am creating a cookie which expires in 30 days..

Here is code in c#

e.UserID --> username,
e.rememberMe --> true


FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
"UserID",
DateTime.Now,
DateTime.Now.AddDays(30),
e.rememberMe,
e.UserID,
FormsAuthentication.FormsCookiePath);
// Encrypt the ticket.
string encTicket = FormsAuthentication.Encrypt(ticket);
// Create the cookie.
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));


But in chrome the cookie store upto browser session ends
enter image description here

Any idea

Answer

You should set the Expires property of the cookie object, because the cookie has no knowledge whatsoever of what you put inside it:

Response.Cookies.Add(
    new HttpCookie(FormsAuthentication.FormsCookieName, encTicket) {
        Expires = ticket.Expiration
    });