azure acive目录验证错误

时间:2017-09-25 08:16:05

标签: azure azure-active-directory

我在azure中遇到了一个Web应用程序的问题。我使用活动目录来控制访问哪个效果很好,但我的一个用户在登录

时收到此错误消息

idx10214受众群体验证失败与validationparameters.validaudience或validationparameters null不匹配

有谁知道这意味着什么?有解决方法吗?

继承人如何设置openid以授权用户,我将如何在此处包含受众设置?观众的价值应该是什么?

app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = AuthorityCHP,
                PostLogoutRedirectUri = postLogoutRedirectUri,
                RedirectUri = loURL,
                Notifications = new OpenIdConnectAuthenticationNotifications()
                {
                    // If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
                    AuthorizationCodeReceived = (context) =>
                    {
                        var code = context.Code;
                        Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential credential = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(clientId, appKey);
                        string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
                        Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(AuthorityCHP, new ADALTokenCache(signedInUserID));
                        var newuri = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path));
                        Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(code, newuri, credential, graphResourceId);
                        return Task.FromResult(0);
                    }
                }
            });

我已经添加了一些额外的代码,希望它可以工作,但是在我部署之后,我让用户有兴趣尝试再次登录,它仍然是相同的。我没有使用webapi它只是一个带有标准登录页面的简单webapp,我真的很难过,为什么只有一个特定的用户对此感兴趣? anynoe可以帮忙吗?

app.UseWindowsAzureActiveDirectoryBearerAuthentication(
        new WindowsAzureActiveDirectoryBearerAuthenticationOptions
        {
            TokenValidationParameters = new TokenValidationParameters
            {
                ValidAudience = clientId
            },
            Tenant = tenantId,
            AuthenticationType = "OAuth2Bearer"
        });

1 个答案:

答案 0 :(得分:0)

这意味着您使用不正确的令牌调用API。当我们调用受Azure AD保护的Web API时,它将验证令牌中的令牌和声明的签名。

audience用于令牌能够访问的资源。我们应该根据资源获取令牌。例如,如果我们使用下面的代码保护网络API,我们应该使用下面的受众群体配置来获取令牌。

public void ConfigureAuth(IAppBuilder app)
{
    app.UseWindowsAzureActiveDirectoryBearerAuthentication(
        new WindowsAzureActiveDirectoryBearerAuthenticationOptions
        {
            Audience = ConfigurationManager.AppSettings["ida:Audience"],
            Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
        });

    //app.UsePasswordAuthentication();
}

更新

TokenValidationParameters = new TokenValidationParameters
{
    ValidateIssuer = false,
    // In a real application you would use IssuerValidator for additional checks, like making sure the user's organization has signed up for your app.
    //     IssuerValidator = (issuer, token, tvp) =>
    //     {
    //        //if(MyCustomTenantValidation(issuer)) 
    //        return issuer;
    //        //else
    //        //    throw new SecurityTokenInvalidIssuerException("Invalid issuer");
    //    },
},