jQuery delete cookie doesn't delete when redirecting

I have multiple cookies with the same name, domain and path, but different values. This is not by design - I am trying to fix this, but cannot delete them. I have tried multiple code variations. Here is one:

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);
}

And here is another:

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);
}

When I list the cookies in Request.Cookies, the "deleted" cookies show up with an expiration date of yesterday, but there are other cookies that show up with an expiration date of 1/1/0001. These are the ones that just won't take a hint and take a hike.

Help greatly appreciated.


Have you tried setting the expire date a year into the past instead of 1 day?

Here is a post on the deleting cookies. In his code he sets the date back 30 years.

Set the domain on the cookies.

  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);
链接地址: http://www.djcxy.com/p/67350.html

上一篇: 本地化连接或动态字符串

下一篇: 重定向时,jQuery删除cookie不会被删除