C#/ASP.NET:无法删除指定了 Domain 属性的 cookie
我的登录方法中有以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,我明白了。
当您删除设置了 Domain 属性的 cookie 时,您需要为新的假 cookie 设置完全相同的属性:
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:
我怀疑您在
Response
位于子域上时设置了Expires
...Crosscheck:您可以尝试从域本身设置它,看看是否有效?
根据 http://msdn.microsoft.com /en-us/library/ms178195%28v=VS.100%29.aspx 您可以通过以下方式删除 cookie:
I suspect you are setting
Expires
while theResponse
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:
需要用空字符串设置域和值。如果没有价值,它就不起作用。
Need to set domain and value with empty string. It does not work without value.
在
.Net 5
响应标头示例
CookieOptions()
是可选的,可以省略,但响应中的 cookie 将不会设置域名。Tested in
.Net 5
response header example
CookieOptions()
are optional and could be omitted, but then cookies in response will not have the domain name set.搜索了相当多的地方,但无法让它工作。
我必须实际设置 value 属性才能使其正常工作!使用 mvc 3 .net 4。
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.