【发布时间】: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