Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem setting cookie expiration to DateTime.Max (December 31, 9999)?

We're supporting legacy code that is dropping a cookie and setting the expiration to DateTime.MaxValue:

HttpCookie cookie = new HttpCookie(cookieName, value);
cookie.Expires = DateTime.MaxValue;

It seems that on some browsers (which we are not logging), this cookie expires immediately--or may not even be getting dropped. According to MSDN, DateTime.MaxValue is December 31, 9999. Are there any browser-related issues with setting a cookie expiration to this date?

The correct answer would be to change the expiration date, but at this point, we can't change production code.

like image 529
SkunkSpinner Avatar asked May 02 '09 01:05

SkunkSpinner


People also ask

How do you set the expiration date on cookies?

This can be done easily by adding expires=expirationDate in UTC separated by semicolon from the name=value, as seen in the following example: document. cookie = "username=Max Brown; expires=Wed, 05 Aug 2020 23:00:00 UTC"; document.

What happen if cookie expires max age is session?

Cookies without an Expires or Max-Age attribute are treated as session cookies, which means they are removed once the browser is closed. Setting a value on either Expires or Max-Age makes them permanent cookies, since they will exist until they hit their expiry date.

How do you set cookies to expire in 30 days?

Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If not set, the cookie will expire at the end of the session (when the browser closes). The path on the server in which the cookie will be available on.

What is Max age in set cookie?

When cookies are set with an explicit Expires/Max-Age attribute the value will now be capped to no more than 400 days in the future. Previously, there was no limit and cookies could expire as much as multiple millennia in the future.


1 Answers

If I had to randomly guess why it's not working, I would say it has something to do with Unix epoch time. This value will technically overflow (on 32-bit machines) after January 19, 2038 at 3:14:07 AM GMT - so the next second after this would be interpreted as January 1, 1970 0:00:01 AM GMT.

It's possible that the future time you've supplied is actually converted into a past time.

Again, this is a complete guess and hopefully I can test it soon when VWD Express 2008 is downloaded on my home machine.

Edit:

Searching for this issue, I have found a similar error:

http://framework.zend.com/issues/browse/ZF-5690

like image 191
John Rasch Avatar answered Sep 26 '22 15:09

John Rasch