【问题标题】:Chrome keeps cookies despite of setting them expired on the server尽管在服务器上将它们设置为过期,但 Chrome 仍保留 cookie
【发布时间】:2018-06-09 02:14:51
【问题描述】:

我在尝试清理多余的 OpenIdConnect.nonce cookie 时遇到了这个问题。当客户端尝试访问受保护的资源(Web 服务)时,OpenIdConnect 中间件会自动添加这些 cookie。设置 cookie 后,中间件会将客户端重定向到身份验证服务,该服务使用 cookie 来实现自身的安全目的。

我的实际问题是我的验收测试收到的“错误请求 - 请求太长”错误,因为请求中充满了几十个随机数 cookie。反过来,发生这种情况是因为我的测试试图在没有适当身份验证的情况下多次访问某些受保护的资源。

合理的决定(在修复测试之前)是通过将过期时间戳设置为过去来删除多余的 cookie:

private void ClearNonceCookies(AuthorizationContext filterContext)
{
    // Clear nonce cookies to prevent the request from growing too big over time
    foreach (var key in filterContext.HttpContext.Request.Cookies.AllKeys.Where(c => c.StartsWith("OpenIdConnect.nonce.")))
    {
        var cookie = filterContext.HttpContext.Response.Cookies[key];

        if (cookie != null)
        {
            cookie.Expires = SystemTime.UtcNow.AddYears(-5);

            filterContext.HttpContext.Response.Cookies.Set(cookie);
        }
    }
}

由于没有这么明显的原因,这不起作用。

【问题讨论】:

    标签: google-chrome firefox cookies


    【解决方案1】:

    我的测试环境使用了一个 http 端点,而它在 Web.config 中也有这个设置:

    <httpCookies httpOnlyCookies="true" requireSSL="true" />
    

    尽管我没有使用 https 绑定,但该行将 Web 服务器配置为使所有 cookie “安全”。

    here 所述,不安全的网站 (http:) 不能再使用“安全”指令设置 cookie(Chrome 52+ 和 Firefox 52+ 中的新功能),因此浏览器忽略了我的过期请求。

    解决方法是将 requireSSL 设置更改为 false 或使用 https 绑定。

    【讨论】:

      猜你喜欢
      • 2011-07-20
      • 2022-08-23
      • 2021-05-05
      • 2014-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-03
      相关资源
      最近更新 更多