Sunday, February 21, 2010

Delete all cookies and session in asp.net

Cookies are one of most important features of web application. From where we store little information on client side in one of my project we need to delete all cookies and as you can not delete cookies from server side so we have to expire them manually. Here is the code for that.

 string[] myCookies = Request.Cookies.AllKeys;
foreach (string cookie in myCookies)
{
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
Same way we can delete all session for following code.
Session.Abandon();
Technorati Tags: ,,,

Shout it
kick it on DotNetKicks.com
Share:

8 comments:

  1. I wish it were that simple. My cookies have this annoying tendency to reappear no matter how hard I try to delete them.

    ReplyDelete
  2. Bob what is the problem you are facing with deleting cookie

    ReplyDelete
  3. same code i use it..but it work in local host only not in actual live site

    ReplyDelete
  4. Kartheeswaran - What's the problem you are having to delete cookie that may be you have not used your cookie domain property.

    ReplyDelete
  5. In my site i use google language translate combo box to change languages,
    i set the cookie value "/en/en" in google translate bar.
    when the user or admin login in my site ,i hide the translate bar and delete cookie...
    it work properly in local host,when launch the website in Server.the code not work ,still cookies are in browser....

    ReplyDelete
  6. Kartheeswaran - I think you have problem with cookie domain property- see following linke.
    http://stackoverflow.com/questions/513782/why-did-asp-net-generate-the-same-cookie-key-for-a-domain-and-subdomain . If you can send me the code then it would be very help full!!

    ReplyDelete
  7. @9f867cef981943a0c40aa457409b74be:disqus - Nice work!!

    ReplyDelete

Your feedback is very important to me. Please provide your feedback via putting comments.

About Me
My Photo

Full Stack Developer,Blogger,Microsoft Mvp, Mentor, Life long learner, 10+ Years of Experience for JavaScript, web and Microsoft technologies. ASP.NET MVC,Web Forms,C#,AngularJs,Golang
Mvp profile
Subscribe to my blog

Enter your email address:

Follow us on facebook
Blog Archive
Total Pageviews