注销时未删除 Cookie

Cookies not deleted on logout

这是我删除所有cookies的方法:

private void ClearSessionAndCookies()
{
    HttpContext.Current.Session.Remove(ExpirySession);

    foreach (string cookieName in HttpContext.Current.Request.Cookies.AllKeys)
    {
        var cookie = new HttpCookie(cookieName)
        {
            HttpOnly = true,
            Secure = Convert.ToInt32(ConfigurationManager.AppSettings["Https"]) == 1,
            Domain = HttpContext.Current.Request.Cookies[cookieName].Domain,
            Expires = DateTime.Now.AddDays(-1D)
        };
        HttpContext.Current.Response.SetCookie(cookie);
    }

    HttpContext.Current.Session.Abandon();
}

它在我的开发计算机上完美运行,其中 IIS 中的所有站点都是不同端口上的本地主机。但是,在 QA 环境中,每个站点都有不同的子域(qa.example.com、mz.example.com、cart.example.com),并且不会删除任何 cookie。

我试着省略设置 cookie.Domain 的行,没有任何改变;尝试使用 Cookies.Add 和 Cookies.Set 以防万一

此外,我在我的经典 ASP 站点 (mz.example.com) 中使用了这段代码,它在我的计算机上也能完美运行:

For Each cookie in Response.Cookies
    Response.Cookies(cookie).Expires = DateAdd("d",-1,now())
Next

在 QA 中它没有,但旧代码有效

Response.AddHeader "Set-Cookie", cookie & "=; Expires = Thu, 01-Jan-70 00:00:01 GMT; Path=/; HttpOnly; " & "Domain =" & Session("Domain")

我是否遗漏了与不同子域相关的内容?所有站点中的域都设置为“.example.com”。

我已经遇到这个注销问题超过两周了,我使用了一些技巧来解决它(url 参数、重定向等)。它花费了我将近 40 个小时的调试、研究和测试。

在主站点的 web.config 文件中,缺少导致所有问题的一个 cookie 的域信息。

<authentication mode="Forms">
  <forms name="problemCookie" defaultUrl="http://qa.example.com/" enableCrossAppRedirects="true" loginUrl="login.aspx" protection="All" path="/" />
</authentication>

添加 domain=".example.com" 解决了问题。