我有多个具有相同名称、域和路径但值不同的 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 的到期日期为 0001 年 1 月 1 日。这些是那些不会接受暗示并徒步旅行的人。
非常感谢帮助。