Chrome 会保留 Cookie,尽管它们在服务器上设置已过期



我在尝试清理冗余的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);
}
}
}

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

我的测试环境使用了 http 端点,同时它在 Web.config 中也有此设置:

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

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

正如这里提到的,不安全的网站(http:(不能再使用"安全"指令设置cookie(Chrome 52+和Firefox 52+中的新功能(,因此浏览器忽略了我的过期请求。

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

最新更新