How to create a non-persistent (in memory) http cookie in C#?

MatthewMartin picture MatthewMartin · Dec 21, 2010 · Viewed 9.4k times · Source

I want my cookie to disappear when the user closes their brower-- I've already set some promising looking properties, but my cookies pop back to live even after closing the entire browser.

HttpCookie cookie = new HttpCookie("mycookie", "abc");
cookie.HttpOnly = true; //Seems to only affect script access
cookie.Secure = true; //Seems to affect only https transport

What property or method call am I missing to achieve an in memory cookie?

Answer

nothrow picture nothrow · Dec 21, 2010
cookie.Expires = DateTime.MinValue;

this cookie will expire, as soon as the browser is closed.