Search code examples
asp.netcookieswebformshttpcookie

Cookies expire return 1/1/0001 12:00:00 AM


How to get cookie date expiration ? I used the following line code to getting it

Request.Cookies("CookieName").Expires.ToString() 

always it return ( Expires: 1/1/0001 12:00:00 AM ) however I had set expire date to it.

varcookie.Expires = DateTime.Now.AddMonths(1)

Solution

  • There is no need to check Expires (as well as Domain and Path) of Cookie object in Request. If Request.Cookies["name"] received it means that it is not expired and sent to correct domain and path.
    In other words, a browser sends only "live" cookie's name: value pair.
    In Response.Cookies you can set Expires date. The cookie survives until that expiration date or a moment one close a browser, whichever occurs last.