Click here to Skip to main content
15,077,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Can any one please tell me how to clear the data stored in cookies through c# coding.

I am trying to refresh the server controls of asp.net page.
Posted
Updated 3-Sep-10 1:22am
v2
Comments
Dalek Dave 3-Sep-10 7:22am
   
Minor Edit for Grammar.

Response.Cookies.Clear() use this implementation to clear all the cookies from the response.
   
Comments
Dalek Dave 3-Sep-10 7:22am
   
Good simple answer.
Believe it or not, Response.Cookies.Clear() is not a reliable way to remove cookie from the Response. To remove the cookie from browser reliably, you need to set the expiry time of the cookie as follows:

C#
/// <summary>
/// Removes Cookie from the response
/// </summary>
/// <param name="Cookie"></param>
private void RemoveCookie(HttpCookie Cookie)
{
    //Cookie is the current cookie that is to be removed.
    Response.Cookies.Remove(Cookie.Name);
    HttpCookie myCookie = new HttpCookie(Cookie.Name);
    myCookie.Expires = DateTime.Now.AddDays(-1d);
    Response.Cookies.Add(myCookie);
}
   
Did you try this[^]?

Google is an amazing search engine and very easy to use.
Learn using it and you will save yourself a lot of time and trouble. :)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900