How to remove all current domain cookies in MVC website?
I am working on a MVC website, and in my logout link I want to remove all the current domain cookies.
I tried this:
this.ControllerContext.HttpContext.Response.Cookies.Clear();
and this:
Response.Cookies.Clear();
but both开发者_如何学运维 didn't work and the cookies still there.
How about this?
string[] myCookies = Request.Cookies.AllKeys;
foreach (string cookie in myCookies)
{
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
What about this ?
if (Request.Cookies["cookie"] != null)
{
HttpCookie myCookie = new HttpCookie("cookie");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Remove(myCookie);
}
myCookie.Expires = DateTime.Now.AddDays(-1d);
This does not clear the cookies instantly.
You can use:
myCookie.Expires = DateTime.Now.AddSeconds(1);
To clear cookies instantly
精彩评论