我在我的 Azure 网站(Azure Active Directory、C#、MVC)上使用 OpenIdConnect 身份验证,并且随机收到此错误
IDX10311: requireNonce is true (default) but validationContext.Nonce is null. A nonce cannot be validated. If you dont need to check the nonce, set OpenIdConnectProtocolValidator.RequireNonce to false
我正在使用 KentorOwinCookieSaver,据我所知,它是这个问题的解决方案,但显然我错了,因为它不断发生。我怎样才能阻止这个?
在ConfigureAuth方法中我有这一行
app.UseKentorOwinCookieSaver();
最佳答案
根据你的描述,我按照这个tutorial并使用了这个code sample检查这个问题。身份验证中间件的初始化如下所示:
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
RedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = context =>
{
context.HandleResponse();
context.Response.Redirect("/Error?message=" + context.Exception.Message);
return Task.FromResult(0);
}
}
});
使用fiddler为了在日志记录时捕获网络跟踪,您可以发现 OpenIdConnect.nonce cookie 将在 OpenID Connect 中间件启动身份验证请求之前发送到浏览器,如下所示:
用户输入凭据并同意权限后,authorization_code
、id_token
、state
将发布到您指定的 RedirectUri ,然后将执行一些验证并生成新的 cookie 并删除以前的 OpenIdConnect.nonce cookie,如下所示:
IDX10311: requireNonce is true (default) but validationContext.Nonce is null. A nonce cannot be validated. If you dont need to check the nonce, set OpenIdConnectProtocolValidator.RequireNonce to false
我用了Microsoft.Owin.Security.OpenIdConnect 3.0.1来测试这个问题。根据我的理解,您需要确保您的 OpenIdConnect.nonce cookie 已成功发送到您的浏览器。例如,如果您的 cookie 发送到 https://localhost:44353/
,而 RedirectUri 设置为 http://localhost:4279
,那么我会遇到类似问题:
或者您可以尝试将 OpenIdConnectProtocolValidator.RequireNonce 显式设置为 false 以禁用检查随机数。
关于c# - OpenIdConnectProtocolValidator - 随机数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48094248/