How do I assign a non-persistent (in-memory) cookie in ASP.NET?

Posted by Jørn Schou-Rode on Stack Overflow See other posts from Stack Overflow or by Jørn Schou-Rode
Published on 2009-11-24T13:39:08Z Indexed on 2010/05/29 16:52 UTC
Read the original article Hit count: 299

Filed under:
|

The following code will send a cookie to the user as part of the response:

var cookie = new HttpCookie("theAnswer", "42");
cookie.Expires = DateTime.Now.AddDays(7);
Response.Cookies.Add(cookie);

The cookie is of the persistent type, which by most browsers will be written to disk and used across sessions. That is, the cookie is still on the client's PC tomorrow, even if the browser and the PC has been closed in between. After a week, the cookie will be deleted (due to line #2).

Non-persistent/in-memory cookies are another bread of cookies, which have a lifespan determined by the duration of the client's browsing session. Usually, such cookies are held in memory, and they are discarded when the browser is closed.

How do I assign an in-memory cookie from ASP.NET?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about cookies