How to delete all expired cookies from CookieCollection?

How to delete all expired cookies from CookieCollection?

+5
source share
3 answers

Try it.

var cookies = Request.Cookies;
foreach (HttpCookie cookie in cookies)
{
    if (cookie.Expires < DateTime.Now)
    {
        Request.Cookies.Remove(cookie.Name);
    }
}

Note the excerpt from MSDN ,

Calling the Remove method from the Cookies collection removes the cookie from the server-side collection, so the cookie will not be sent to the client. However, the method does not remove the cookie from the client if it already exists.

+2
source

cookie . cookie, cookie . , , cookie, , cookie .

if (Request.Cookies["UserSettings"] != null)
{
    HttpCookie myCookie = new HttpCookie("UserSettings");
    myCookie.Expires = DateTime.Now.AddDays(-1);//add -1 days
    Response.Cookies.Add(myCookie);
}
+1

How do you delete a cookie is to change the expiration date to some thing that has already passed .. , but in the first case, the cookie is never sent from the browser ...

0
source

All Articles