开发者

How to remove all current domain cookies in MVC website?

开发者 https://www.devze.com 2023-03-13 20:10 出处:网络
I am working on a MVC website, and in my logout link I want to remove all the current domain cookies.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消