C#/ASP.NET:无法删除指定了 Domain 属性的 cookie

发布于 2024-11-28 21:42:06 字数 374 浏览 0 评论 0原文

我的登录方法中有以下代码:

Response.Cookies["cookie"].Value = "...";
Response.Cookies["cookie"].Domain = "domain.com";

这样,cookie 就会被放入主域和所有子域中

但是,当我尝试删除 cookie 时:

Response.Cookies["cookie"].Expires = DateTime.Now.AddYears(-1);

它不起作用!

当我删除指定 Domain 属性的 2 行代码时,它工作正常。

我该如何解决这个问题?

谢谢

I have the following code in my login method:

Response.Cookies["cookie"].Value = "...";
Response.Cookies["cookie"].Domain = "domain.com";

This way the cookie is put into the main domain and all subdomains

However when I try to remove the cookies:

Response.Cookies["cookie"].Expires = DateTime.Now.AddYears(-1);

It doesn't work!

When I remove the 2 line of code where Domain property is specified, it works fine.

How can I solve this problem?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甩你一脸翔 2024-12-05 21:42:06

好吧,我明白了。

当您删除设置了 Domain 属性的 cookie 时,您需要为新的假 cookie 设置完全相同的属性:

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

Okay, I figured that out.

When you remove a cookie with Domain property set, you need to set the very same property for the new fake cookie:

if (Request.Cookies["cookie"] != null)
{
    HttpCookie myCookie = new HttpCookie("cookie");
    myCookie.Expires = DateTime.Now.AddDays(-1d);
    myCookie.Domain = "domain.com"; // !!!!
    Response.Cookies.Add(myCookie);
}
丶情人眼里出诗心の 2024-12-05 21:42:06

我怀疑您在 Response 位于子域上时设置了 Expires...
Crosscheck:您可以尝试从域本身设置它,看看是否有效?

根据 http://msdn.microsoft.com /en-us/library/ms178195%28v=VS.100%29.aspx 您可以通过以下方式删除 cookie:

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

I suspect you are setting Expires while the Response is on a subdomain...
Crosscheck: Can you try and set it from the domain itself and see if that works ?

According to http://msdn.microsoft.com/en-us/library/ms178195%28v=VS.100%29.aspx you can delete a cookie by:

if (Request.Cookies["cookie"] != null)
{
    HttpCookie myCookie = new HttpCookie("cookie");
    myCookie.Expires = DateTime.Now.AddDays(-1d);
    Response.Cookies.Add(myCookie);
}
初与友歌 2024-12-05 21:42:06

需要用空字符串设置域和值。如果没有价值,它就不起作用。

        var cookie = new HttpCookie(cookieName, string.Empty)
        {
            Expires = DateTime.Now.AddYears(-1),
            Domain = {YourDomain}
        };

        Response.Cookies.Add(cookie);

Need to set domain and value with empty string. It does not work without value.

        var cookie = new HttpCookie(cookieName, string.Empty)
        {
            Expires = DateTime.Now.AddYears(-1),
            Domain = {YourDomain}
        };

        Response.Cookies.Add(cookie);
晚雾 2024-12-05 21:42:06

.Net 5

        foreach (var cookie in HttpContext.Request.Cookies)
        {
            Response.Cookies.Delete(cookie.Key, new CookieOptions()
            {
                Domain = Request.Host.Host // ADD
            });
        }

响应标头示例

中进行测试

设置cookie:_example=;过期=1970 年 1 月 1 日星期四 00:00:00 GMT;域=您的.DOMAIN;路径=/

CookieOptions() 是可选的,可以省略,但响应中的 cookie 将不会设置域名。

Tested in .Net 5

        foreach (var cookie in HttpContext.Request.Cookies)
        {
            Response.Cookies.Delete(cookie.Key, new CookieOptions()
            {
                Domain = Request.Host.Host // ADD
            });
        }

response header example

set-cookie: _example=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=YOUR.DOMAIN; path=/

CookieOptions() are optional and could be omitted, but then cookies in response will not have the domain name set.

岁月蹉跎了容颜 2024-12-05 21:42:06

搜索了相当多的地方,但无法让它工作。
我必须实际设置 value 属性才能使其正常工作!使用 mvc 3 .net 4。

        var current = HttpContext.Current.Request.Cookies[key];
        if (current == null) return;
        var myCookie = new HttpCookie(key)
                           {
                               Expires = DateTime.Now.AddYears(-1),
                               Value = current.Value,
                               Domain = domain
                           };
        HttpContext.Current.Response.Cookies.Set(myCookie);

Searched around quite a bit and couldn't get it to work.
I had to actually set the value property to make it work! Using mvc 3 .net 4.

        var current = HttpContext.Current.Request.Cookies[key];
        if (current == null) return;
        var myCookie = new HttpCookie(key)
                           {
                               Expires = DateTime.Now.AddYears(-1),
                               Value = current.Value,
                               Domain = domain
                           };
        HttpContext.Current.Response.Cookies.Set(myCookie);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文