注销时未删除Cookie

时间:2015-11-20 18:28:19

标签: asp.net cookies asp-classic single-sign-on

这是删除所有Cookie的方法:

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中的所有站点都是不同端口上的localhost。但是,在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

在质量保证中,它没有,但旧代码正常工作

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

我错过了与不同子域相关的内容吗?所有网站中的域都设置为" .example.com"。

1 个答案:

答案 0 :(得分:0)

我已经有超过两周的退出问题,并且我已经使用了一些黑客来克服它(网址参数,重定向等)。我花了将近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"解决了这个问题。