jam*_*mes 0 c# azure-active-directory asp.net-core-mvc openid-connect
因此,我有一个新创建的netcore应用程序,它通过中间件设置链接到我的azure Active Directory帐户,如下所示:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
ClientId = Configuration["Authentication:AzureAd:ClientId"],
Authority = Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"],
CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"],
Events = new OpenIdConnectEvents
{
OnAuthenticationFailed = OnAuthenticationFailed,
OnAuthorizationCodeReceived = OnAuthorizationCodeReceived
}
});
Run Code Online (Sandbox Code Playgroud)
我的回调路径默认为“ CallbackPath”:“ / signin-oidc”,我在URL上的天蓝色符号为http:// localhost:20352 / ,回复URL为http:// localhost:20352 / signin-oidc
现在,我可以正常进行登录过程了,但是如果您按几次浏览器后退按钮,我将被炸死:
处理请求时发生未处理的异常。
例外:关联失败。位置不明
AggregateException:未处理的远程故障。(关联失败。)Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1.d__5.MoveNext()
如何或在何处捕获此异常以进行相应处理?或导致此问题的中间件设置有问题。
上面的两个事件从未发生过。
编辑:可能有助于了解来自浏览器后退按钮的爆炸URL是“ http:// localhost:20352 / signin-oidc ”
这显然不存在为有效的控制器/动作路线
单击“后退”按钮时出现此错误是由于浏览器重新使用对身份提供者的Authorize端点的调用,而旧的Nonce和State参数不再有效。
当您的应用程序发出身份验证质询并启动到身份提供者的重定向时,它会临时存储这些值,以便可以验证在回调中获得的响应和令牌是否有效。这就是“相关性”的含义。
未调用事件处理程序的原因是您想改为处理RemoteFailure事件,例如,重定向到错误屏幕:
public class AuthenticationEvents : OpenIdConnectEvents
{
public override Task RemoteFailure(FailureContext context)
{
context.HandleResponse();
context.Response.Redirect("/Home/Error");
return Task.FromResult(0);
}
}
Run Code Online (Sandbox Code Playgroud)