asp.net-mvc 如何删除 MVC 网站中的所有当前域 cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6403978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:16:49  来源:igfitidea点击:

How to remove all current domain cookies in MVC website?

asp.net-mvc

提问by Amr Elgarhy

I am working on a MVC website, and in my logout link I want to remove all the current domain cookies.

我在 MVC 网站上工作,在我的注销链接中,我想删除所有当前域 cookie。

I tried this:

我试过这个:

this.ControllerContext.HttpContext.Response.Cookies.Clear();

and this:

和这个:

Response.Cookies.Clear();

but both didn't work and the cookies still there.

但两者都不起作用,饼干还在那里。

回答by Swift

How about this?

这个怎么样?

string[] myCookies = Request.Cookies.AllKeys;
foreach (string cookie in myCookies)
{
  Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}

回答by Sunil Acharya

What about this ?

那这个呢 ?

    if (Request.Cookies["cookie"] != null)
    {
        HttpCookie myCookie = new HttpCookie("cookie");
        myCookie.Expires = DateTime.Now.AddDays(-1d);
        Response.Cookies.Remove(myCookie);
    }