How can I remove all my site's cookies from the client, either in ASP.NET/C# or JavaScript? Basically I would like to click a button or link on the page and have it clear all the cookies for the site. I don't need to know the name of every cookie, do I?
+2
A:
As I wrote before to @mwilson, you cannot directly delete a cookie on a user's computer. However, you can direct the user's browser to delete the cookie by setting the cookie's expiration date to a past date.
To delete all cookies just browse the Response.Cookies collection with a foreach loop and replace the date of each cookie with a past date.
Check this page if you need some help:
backslash17
2010-07-08 21:52:47
+1, hadn't thought of doing it that way.
mwilson
2010-07-08 22:01:26
+1
A:
foreach (string key in Request.Cookies.AllKeys)
{
HttpCookie cookie = new HttpCookie(key);
cookie.Expires = DateTime.UtcNow.AddDays(-7);
Response.Cookies.Add(cookie);
}
LukeH
2010-07-08 21:59:05