and*_*fox 16 asp.net cookies oauth owin
我Microsoft.Owin.Security.OpenIdConnect在C#ASP MVC Web应用程序中使用OWIN/OAuth和OpenId Connect身份验证().使用Microsoft帐户登录SSO基本上可以正常工作,但我不时会在浏览器上显示错误页面Bad Request - Request Too Long.
我发现这个错误是由太多的cookie引起的.删除cookie有一段时间了,但过了一段时间后问题又回来了.
导致问题的cookie是从OpenId框架设置的,所以有几十个名字像的cookie OpenIdConnect.nonce.9oEtF53WxOi2uAw........
这不是SPA应用程序,但有些部分会使用ajax调用定期刷新.
and*_*fox 37
事实证明,根本原因是Ajax调用.
有问题的流程是
1)OAuth cookie在一段时间后过期了
2)到期通常会导致页面重定向login.microsoft.com以刷新cookie.在此步骤中,OAuth框架会 nonce在响应中添加新 cookie(每次)!
3)但是Ajax不处理域外的重定向(跨域login.microsoft.com).但cookie已经附加到页面上.
4)下一个周期性Ajax调用重复该流程,导致'nonce'cookie快速增加.
解
我不得不扩展"OWIN OpenId"框架设置代码以不同方式处理Ajax调用 - 以防止重定向并停止发送cookie.
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
Notifications = new OpenIdConnectAuthenticationNotifications
{
RedirectToIdentityProvider = ctx =>
{
bool isAjaxRequest = (ctx.Request.Headers != null && ctx.Request.Headers["X-Requested-With"] == "XMLHttpRequest");
if (isAjaxRequest)
{
ctx.Response.Headers.Remove("Set-Cookie");
ctx.State = NotificationResultState.HandledResponse;
}
return Task.FromResult(0);
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
还必须调整Ajax调用程序以检测401代码并执行完整页面刷新(这导致快速重定向到Microsoft权限).
小智 7
对我来说,解决方案是强制创建 ASP.NET 会话。
重现步骤:
解决方案:通过添加强制创建会话
protected void Session_Start(object sender, EventArgs e)
{
}
Run Code Online (Sandbox Code Playgroud)
到 global.asax.cs。
| 归档时间: |
|
| 查看次数: |
8185 次 |
| 最近记录: |