Skip to content Skip to sidebar Skip to footer

C# Asp.net Getting Cookie Expiration Time Set In Javascript Always Returns 01.01.0001

I use a javascript function to store the cookie: createCookie('teaser', 'teaser', 7); function createCookie(name, value, days) { var expires = ''; if (days) { var date = new Da

Solution 1:

Egghead says that:

The browser is responsible for managing cookies, and the cookie's expiration time and date help the browser manage its store of cookies. Therefore, although you can read the name and value of a cookie, you cannot read the cookie's expiration date and time. When the browser sends cookie information to the server, the browser does not include the expiration information. (The cookie's Expires property always returns a date-time value of zero.) If you are concerned about the expiration date of a cookie, you must reset it.


Solution 2:

The browser does not transmit the expiration date of a cookie to servers, this is as per HTTP specification.

Browsers only send the cookie name and value only.

The Expires property on the cookie object is only used when setting expiration date on a cookie that is going to be written in a response, not read in a request.

When reading the cookie is Expires property value is always going to be null. You can see this for yourself by examining the HTTP headers. If you really want to read the expire value of a cookie, try writing it in the value itself, or using another cookie with the value of the expiration date (you will need to keep them in sync yourself).


Post a Comment for "C# Asp.net Getting Cookie Expiration Time Set In Javascript Always Returns 01.01.0001"