我有多个具有相同名称,域和路径但不同值的Cookie。这不是设计 - 我试图解决这个问题,但不能删除它们。我尝试了多种代码变体。这是一个:
string[] cookies = Request.Cookies.AllKeys;
HttpCookie cookie;
string cookieName;
string cookieValue;
for (int i = 0; i < cookies.Count(); i++)
{
cookieName = Request.Cookies[i].Name;
if (cookieName == "ASP.NET_SessionId")
{
// Do not delete session cookie or we will be logged out
continue;
}
cookieValue = Request.Cookies[i].Value;
cookie = new HttpCookie(cookieName);
cookie.Value = "";
cookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(cookie);
}
这是另一个:
string[] cookies = Request.Cookies.AllKeys;
for (int i = 0; i < Request.Cookies.AllKeys.Count(); i++)
{
if (Request.Cookies[i].Name == "ASP.NET_SessionId")
{
// Do not delete session cookie or we will be logged out
continue;
}
Request.Cookies[i].Expires = DateTime.Now.AddDays(-1);
}
当我在Request.Cookies中列出cookie时,“已删除”的cookie显示的是截止日期为昨天,但是还有其他cookie显示有效期为1/1/0001。这些是不会暗示并加息的。
非常感谢。
答案 0 :(得分:1)
您是否尝试将过期日期设置为过去一年而不是1天?
以下是deleting cookies上的帖子。在他的代码中,他将日期定为30年。
在Cookie上设置域名。
cookieValue = Request.Cookies[i].Value;
cookie = new HttpCookie(cookieName);
cookie.Value = "";
cookie.Domain = "dev.domain.com";
cookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(cookie);