【问题标题】:OpenIdConnectProtocolValidator - nonce errorOpenIdConnectProtocolValidator - 随机数错误
【发布时间】:2018-06-14 02:57:53
【问题描述】:

我在我的天蓝色网站(天蓝色活动目录、c#、MVC)上使用 OpenIdConnect 身份验证,但我随机收到此错误

IDX10311:requireNonce 为 true(默认)但 validationContext.Nonce 一片空白。无法验证随机数。如果您不需要检查 nonce,将 OpenIdConnectProtocolValidator.RequireNonce 设置为 false

我正在使用 KentorOwinCookieSaver,据我所知,这是解决此问题的方法,但显然我错了,因为它一直在发生。我怎样才能阻止这种情况?

在 ConfigureAuth 方法中我有这一行

app.UseKentorOwinCookieSaver();

【问题讨论】:

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


    【解决方案1】:

    根据您的描述,我关注了这个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 为 true(默认)但 validationContext.Nonce 为空。无法验证随机数。如果不需要检查 nonce,请将 OpenIdConnectProtocolValidator.RequireNonce 设置为 false

    我使用Microsoft.Owin.Security.OpenIdConnect 3.0.1 来测试这个问题。据我了解,您需要确保您的 OpenIdConnect.nonce cookie 已成功发布到您的浏览器。例如,如果你的 cookie 发给https://localhost:44353/,而 RedirectUri 设置为http://localhost:4279,那么我会遇到类似的问题:

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

    【讨论】:

    • 当我们禁用它时,它会产生不同的错误。 IDX21329: RequireState is '[PII is hidden]' but the OpenIdConnectProtocolValidationContext.State is null. State cannot be validated.
    • 我们也尝试过禁用状态,但仍然出现同样的错误。 ProtocolValidator = new OpenIdConnectProtocolValidator() {RequireNonce = false, RequireState = false}
    • 在我的情况下,我可以在浏览器中看到 OpenIdConnect.nonce 但问题是我收到了这个 cors 错误:Access to XMLHttpRequest at 'https://devsafeplatformxxxx.b2clogin.com/devsafeplatformxxxx.onmicrosoft.com/b2c_1_sign_in/oauth2/v2.0/authorize?...' (redirected from 'https://www.xx.com/api/test/getdata') from origin 'https://www.xx.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
    猜你喜欢
    • 2019-10-22
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 2016-05-23
    相关资源
    最近更新 更多