Reading Cookie Expiration Date

How to get cookie expiration date / creation date from javascript?

The information is not available through document.cookie, but if you're really desperate for it, you could try performing a request through the XmlHttpRequest object to the current page and access the cookie header using getResponseHeader().

Reading cookie expiration date

It is not possible to get the expiration date of a cookie through Javascript; only key-value pairs are exposed through document.cookie.

Cookie Expiry Date Not Set Correctly in Javascript

This is due to Safari's Intelligent Tracking Prevention. It limits certain cookies to 7 day expiration. From What Restricts Does ITP Place on Cookies and Other Web Browser Storage?:

  • First-party cookies created by JavaScript’s document.cookie will expire in 7 days. If the cookies are accessed within those 7 days, then their expiration date will be extended by 7 days.
  • First-party cookies created by JavaScript’s document.cookie AND are created by a tracking domain AND use link decoration will expire in 24 hours. If the cookies are accessed within 24 hours, then their expiration date will be extended by another 24 hours.

So even though the expiration is set to 1 week, if the user continues to access the site that uses the cookie, it will be extended automatically.

Why is the cookie expiration date not surviving across sessions in ASP.NET?

The Short Answer - You cannot read the cookie's expiration date and time.

Slightly Longer Answer - This is not an issue of sessions in ASP.NET. It is an issue of what you can read from a cookie server-side in ASP.NET. Per the MSDN:

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.)

You can read the Expires property of a cookie that you have set in the
HttpResponse object, before the cookie has been sent to the browser.
However, you cannot get the expiration back in the HttpRequest object.

So basically, the cookie expiration date is set correctly. This can be verified by inspecting the cookie in the browser. Unfortunately, reading this cookie like in your Get function will return 1/1/0001.

If you really want to get the expiration, then you'd have to store it in the cookie itself:

Set

DateTime exp = DateTime.Now.AddDays(1);
HttpCookie PreferredCookie = new HttpCookie("PreferredCookie");
PreferredCookie.Values.Add("cookieType", "Zref");
PreferredCookie.Values.Add("exp", exp.ToString());
PreferredCookie.Expires = exp;
Response.Cookies.Set(PreferredCookie);

Get

HttpCookie PreferredCookie = Request.Cookies["PreferredCookie"];
if (PreferredCookie != null)
{
CookieLiteral.Text = "Value = " + PreferredCookie["cookieType"] + "<br>";
CookieLiteral.Text += "Expires = " + PreferredCookie["exp"];
}
else
{
CookieLiteral.Text = "No Cookie";
}


Related Topics



Leave a reply



Submit