Click here to Skip to main content
15,908,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to clear cookies after logout
Posted

to get the browser to dump the cookies
you have to set the cookies to expire in the past.
In the LogOut event just :
C#
if ((Request.Cookies("CookieName") != null))
{ 
  Response.Cookies("CookieName").Expires = DateTime.Now.AddDays(-30);
}

In the above in AddDays Specify no. of days by keeping in mind what you you have used while creating cookie.

for more refer :http://wiki.asp.net/page.aspx/372/login-through-cookies/[^]

http://msdn.microsoft.com/en-us/library/ms178195.aspx[^]
 
Share this answer
 
v4
Hello friend

Deleting a cookie—physically removing it from the user's hard disk—is a variation on modifying it. You cannot directly remove a cookie because the cookie is on the user's computer. However, you can have the browser delete the cookie for you. The technique is to create a new cookie with the same name as the cookie to be deleted, but to set the cookie's expiration to a date earlier than today. When the browser checks the cookie's expiration, the browser will discard the now-outdated cookie. The following code example shows one way to delete all the cookies available to the application:

C#
HttpCookie aCookie = default(HttpCookie);
int i = 0;
string cookieName = null;
int limit = Request.Cookies.Count - 1;
for (i = 0; i <= limit; i++) {
  cookieName = Request.Cookies(i).Name;
  aCookie = new HttpCookie(cookieName);
  aCookie.Expires = DateTime.Now.AddDays(-1);
  Response.Cookies.Add(aCookie);
}
 
Share this answer
 
public static void ClearCookies()
        {

            if (HttpContext.Current.Request.Cookies["Users"] != null)
            {
                HttpCookie aCookie = HttpContext.Current.Request.Cookies["Users"];
                aCookie.Expires = DateTime.Now.AddDays(-10);
                aCookie.Value = "";
                HttpContext.Current.Response.Cookies.Add(aCookie);
            }

        }
 
Share this answer
 
v2
Hi,

Use this:

C#
Response.Cookies.Clear();
//this will clear all the cookies created by the page.

Or
C#
Response.Cookies["MyCookie"].Expires = DateTime.Now.AddDays(-5);
//this will clear a particular cookie created by the page.


All the best.
--AK
 
Share this answer
 
C#
public static void RemoveCookie(string key)
        {
            //get cookies value
            HttpCookie cookie = null;
            if (HttpContext.Current.Request.Cookies[key] != null)
            {
                cookie = HttpContext.Current.Request.Cookies[key];
                
                //set its expiry date to earlier date
                cookie.Expires = DateTime.Now.AddDays(-1);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }


        }
 
Share this answer
 

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