C# 使用Azure B2C进行.NET核心JWT验证
我对C# 使用Azure B2C进行.NET核心JWT验证,c#,azure,azure-ad-b2c,C#,Azure,Azure Ad B2c,我对Azure B2C的令牌验证有问题。错误是: 身份验证失败:IDX10205:颁发者验证失败。发行人: “[PII在默认情况下是隐藏的。请在中设置'ShowPII'标志。” IdentityModelEventSource.cs设置为true以显示它。]'。不匹配: validationParameters.ValidisUser:“[PII在默认情况下是隐藏的。请设置 IdentityModelEventSource.cs中的“ShowPII”标志设置为true以显示它。]” 或valid
Azure B2C
的令牌验证有问题。错误是:
身份验证失败:IDX10205:颁发者验证失败。发行人:
“[PII在默认情况下是隐藏的。请在中设置'ShowPII'标志。”
IdentityModelEventSource.cs设置为true以显示它。]'。不匹配:
validationParameters.ValidisUser:“[PII在默认情况下是隐藏的。请设置
IdentityModelEventSource.cs中的“ShowPII”标志设置为true以显示它。]”
或validationParameters.ValidisUsers:“[PII在默认情况下是隐藏的。设置
IdentityModelEventSource.cs中的“ShowPII”标志设置为true以显示
是的
在我的.netcore
webapi
项目中,我从Nuget添加了System.IdentityModel.Tokens.Jwt。然后在Startup.cs的ConfigureServices
I下添加:
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);
}
其中B2Ctenadid
类似于mytenant.onmicrosoft.com
,B2CClientId
是来自Azure门户的if,而B2CPolicy
是b2c\u 1\u susi
(为登录而创建)
正确的配置是
var cnn = $"https://login.microsoftonline.com/tfp/{Configuration["B2CTenantId"]}/" +
$"{Configuration["B2CPolicy"]}/v2.0/";
B2CTenant
是您的域,类似于mydomain.onmicrosoft.com
。
B2CPolicy
是您在Azure Portal
中创建的策略(请参见问题中的我的图片)。好的,url应该是$”https://login.microsoftonline.com/{配置[“B2CTenantId”]}/v2.0
在令牌兼容性设置下
有我正在使用的url,它是颁发者,而不是颁发机构。正确的连接是https://login.microsoftonline.com/tfp/{Configuration[“B2CTenantId”]}/B2C_1_susi/v2.0/
您可以从中获得详细步骤。