c# - OpenIdConnectProtocolValidator - 随机数错误

标签 c# asp.net-mvc azure openid-connect nonce

我在我的 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 中间件启动身份验证请求之前发送到浏览器,如下所示:

enter image description here

用户输入凭据并同意权限后,authorization_codeid_tokenstate 将发布到您指定的 RedirectUri ,然后将执行一些验证并生成新的 cookie 并删除以前的 OpenIdConnect.nonce cookie,如下所示:

enter image description here

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,那么我会遇到类似问题:

enter image description here

或者您可以尝试将 OpenIdConnectProtocolValidator.RequireNonce 显式设置为 false 以禁用检查随机数。

关于c# - OpenIdConnectProtocolValidator - 随机数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48094248/

相关文章:

c# - 我需要不带 XAML 的 WPF 控件示例

c# - 具有自引用功能的 Fluent NHibernate 自动映射

azure - 有没有办法使用 GCP 资源将数据从 Google Cloud Storage 传输到 Azure Blob Storage?

Azure 事件中心 : Receive events as a batch that were sent to hub as individual messages?

c# - 报表查看器 - 请求 SqlClientPermission 类型的权限失败

c# - WPF - 更改组合框中枚举的字符串值

asp.net-mvc - 具有可为空 Guid 的 Web Api GET 方法可能吗?

asp.net-mvc - 如何创建允许在 asp.net mvc jQuery 中搜索的多选列表框

jquery - 将网页设置为只读?

python - 将azure secret 变量传递给管道中的pytest?