我对Azure B2C
的令牌验证有问题。错误是:
AuthenticationFailed:IDX10205:颁发者验证失败。发行人:'[PII默认隐藏。将IdentityModelEventSource.cs中的'ShowPII'标志设置为true以显示它。]'。不匹配:validationParameters.ValidIssuer:'[PII默认隐藏。将IdentityModelEventSource.cs中的'ShowPII'标志设置为true以显示它。]'或validationParameters.ValidIssuers:'[PII默认隐藏。将IdentityModelEventSource.cs中的'ShowPII'标志设置为true以显示它。]'。
在我的.net core
webapi
项目中,我从Nuget添加了System.IdentityModel.Tokens.Jwt。然后在ConfigureServices
下的Startup.cs中我添加了:
services.AddAuthentication(options =>
{
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(jwtOptions =>
{
jwtOptions.Authority =
$"https://login.microsoftonline.com/tfp/{Configuration["B2CTenantId"]}/" +
"{Configuration["B2CPolicy"]}/v2.0/";
jwtOptions.Audience = Configuration["B2CClientId"];
jwtOptions.Events = new JwtBearerEvents
{
OnAuthenticationFailed = AuthenticationFailed
};
});
private Task AuthenticationFailed(AuthenticationFailedContext arg)
{
// For debugging purposes only!
var s = $"AuthenticationFailed: {arg.Exception.Message}";
arg.Response.ContentLength = s.Length;
arg.Response.Body.Write(Encoding.UTF8.GetBytes(s), 0, s.Length);
return Task.FromResult(0);
}
其中B2CTenandId
类似于mytenant.onmicrosoft.com
,B2CClientId
是来自Azure门户的if和B2CPolicy
是b2c_1_susi
(为登录创建)。
正确的配置是
var cnn = $"https://login.microsoftonline.com/tfp/{Configuration["B2CTenantId"]}/" +
$"{Configuration["B2CPolicy"]}/v2.0/";
B2CTenant
是你的域名mydomain.onmicrosoft.com
。 B2CPolicy
是您在Azure Portal
创建的政策(请参阅我在问题中的图片)。