Search code examples
c#cookiessetcookie

cookie.Expire is not working in chrome


Hi i come across very strange Problem in chrome.I have following code for Cookie.

 HttpCookie cookie = new HttpCookie("cookie");
 cookie.Value = "true";
 cookie.Expires.AddDays(30);
 Response.Cookies.Add(cookie);

Now above code is not working in chrome while its working well in FF and IE.If i check this cookie in chrome then its there but cookie Expire date is like When the browsing session ends in chrome.

that means if i Close the Browser then the cookie will expire instead of expiring after 30 days.


Solution

  • You have to use DateTime.Now/DateTime.UtcNow:

    HttpCookie cookie = new HttpCookie("cookie");
    cookie.Value = "true";
    cookie.Expires = DateTime.UtcNow.AddDays(30);
    Response.Cookies.Add(cookie);