【发布时间】:2021-07-05 23:27:57
【问题描述】:
我有一个设置了应用注册的 Azure Active Directory。我有一个 B2C 租户,其中包含应用注册并设置了多个用户流。
在 Startup.cs 中,我配置了多个身份验证方案,如下所示。
services
.AddAuthentication()
.AddJwtBearer("Azure", options =>
{
options.Audience = "clientId";
options.Authority = "https://login.microsoftonline.com/tenantId";
})
.AddJwtBearer("AzureB2cCustomerSignIn", options =>
{
options.Audience = "clientIdInB2c";
options.Authority = "https://tentantName.b2clogin.com/tenantName.onmicrosoft.com/userFlowName/v2.0";
})
.AddJwtBearer("AzureB2cCustomerSignUp", options =>
{
options.Audience = "clientIdInB2c";
options.Authority = "https://tentantName.b2clogin.com/tenantName.onmicrosoft.com/userFlowName/v2.0";
})
.AddJwtBearer("AzureB2cEmployeeSignUpAndSignIn", options =>
{
options.Audience = "clientIdInB2c";
options.Authority = "https://tentantName.b2clogin.com/tenantName.onmicrosoft.com/userFlowName/v2.0";
});
services
.AddAuthorization(options =>
{
options.DefaultPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.AddAuthenticationSchemes("Azure", "AzureB2cCustomerSignIn", "AzureB2cCustomerSignUp", "AzureB2cEmployeeSignUpAndSignIn")
.Build();
}
当我尝试使用来自 B2C 流的令牌进行身份验证时,我收到了以下单个错误。应用程序可以继续,API 方法被执行,一切正常。但这会严重混淆应用程序洞察力和错误。更重要的是,很可能还会影响性能。我一定是缺少一些配置...
Microsoft.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException: IDX10501: Signature validation failed. Unable to match key:
kid: 'System.String'.
Exceptions caught:
'System.Text.StringBuilder'.
token: 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken'.
at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateSignature(String token, TokenValidationParameters validationParameters)
at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken(String token, TokenValidationParameters validationParameters, SecurityToken& validatedToken)
at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler: Information: Azure was not authenticated. Failure message: IDX10501: Signature validation failed. Unable to match key:
kid: 'System.String'.
Exceptions caught:
'System.Text.StringBuilder'.
token: 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken'.
它指出无法根据我为 Azure Active Directory 配置的身份验证方案验证令牌,这很明显。由于它是来自 B2C 身份验证流程的令牌,我什至不希望它尝试使用此身份验证方案进行身份验证。从逻辑上讲,当我使用来自 AAD 流的令牌进行身份验证时,我得到了三次 SecurityTokenSignatureKeyNotFoundException。
我怎样才能避免这个错误?
【问题讨论】:
标签: asp.net-core