views:

731

answers:

2

How to clear the cookies that has been stored through my asp.net mvc(C#) application, when the user closes the browser?

Is there any option to create a cookie such that it expires once the browser closed?

I need to use cookies, because i will store some of the values to be maintained until the browser is closed.

For example, During sign in i may store the userid in cookie, which i can use for my application processes till the bwoser closes.

Session will expire after some particular time, which i need to overcome with using cookies

A: 

Sessions are usualy used for this. According to Wikipedia, when no expiration date is set, a cookie is cleared when the user closes the browser.

The cookie setter can specify a deletion date, in which case the cookie will be removed on that date. If the cookie setter does not specify a date, the cookie is removed once the user quits his or her browser.

Ikke
The session will expire when the user keeps the browser open for long time. Will the cookie with no expiry date lasts until the browser closes?
Prasad
It really depends on how the browser has implemented it.
Ikke
A: 

As mentioned in this SO question:

Response.Cookies("cookie_name").Expires = Session.Timeout;
Kaleb Brasee
You actually want to specify no Expires value on the cookie. This will cause the cookie to be removed when the browser session ends (user closes the browser).
Sean Carpenter