.net HttpCookie class / session cookie issues

I am wondering how to make a regular HttpCookie object into a cookie that expires at the end of the session. I'm not interested in someone showing me HttpContext.Session . How is a session cookie viewed in response headers compared to a regular cookie? How can I change an HttpCookie that expires at the end of a session? Thanks!

+6
cookies session session-state
source share
3 answers

A session cookie is just a cookie that does not have a set expiration date.

 Response.Cookies.Add(new HttpCookie("name", "value")); 

or

 Response.Cookies["name"] = "value"; 
+14
source

The cookie with the expiration of DateTime.MinValue (1/1/0001) expires at the end of the session. This is the default expiration date for the cookie in asp.net.

You can force the cookie to be removed from the client by setting the expiration date to something earlier than "now" (DateTime.Now.AddDays (-1d)), in which case it will be deleted when the client hits.

If we had null types when the HttpCookie was encoded, my hunch is that the null date will be equal to the session-based cookie, and everything else will be converted to expiration, but that is not the case.

+7
source

Cookie Expiration:

  • Session cookie - end date must be DateTime.MinValue, which is 1/1/0001 00:00:00
  • Normal cookie (limited time) - end date - any future date equal to or greater than the current date of Date.Now.
  • Remote cookie - anytime between DateTime.MinValue and DateTime.Now.

To change the cookie in a session cookie, simply assign a MinValue.

 httpCookie.Expires = DateTime.MinValue; 

If your cookie is new. The default value for DateTime should be DateTime.MinValue and should not be set.

CallMeLaNN

+5
source

All Articles