Azure ad b2c 多个自定义B2C策略,如何在API中处理?
我们将Azure B2C与多个自定义策略一起使用,以支持两种“正常”登录 还可以注册流量作为系统的邀请Azure ad b2c 多个自定义B2C策略,如何在API中处理?,azure-ad-b2c,azure-ad-b2c-custom-policy,Azure Ad B2c,Azure Ad B2c Custom Policy,我们将Azure B2C与多个自定义策略一起使用,以支持两种“正常”登录 还可以注册流量作为系统的邀请 “B2C_1A_sign”:只是一个正常的登录流 “B2C_1A_BRS_signingup”:用户接受som TOC(简化)的邀请流 我的问题是,当有两个策略时,如何在受保护的API应用程序中处理这种情况。 从下面的代码中可以看到,我们为每个B2C策略调用方法AddJwtBearer,结果执行每个相应的回调OnTokenValidated,结果是我们添加了两个claimsidentity
- “B2C_1A_sign”:只是一个正常的登录流
- “B2C_1A_BRS_signingup”:用户接受som TOC(简化)的邀请流
AddJwtBearer
,结果执行每个相应的回调OnTokenValidated
,结果是我们添加了两个claimsidentity。
所以我的问题是,处理这种情况的正确方法是什么?乍一看,在配置策略“PolicyNames.UserAccessPolicy”时,可能只指定一个身份验证模式,但这种方法不起作用,我无法提前知道用户是如何登录的
(在我们的案例中,合并我们的两个自定义策略不是一个选项)
问候尼古拉斯
services.AddAuthentication()
.AddJwtBearer("B2C_1A_Signin", options =>
{
options.Authority = $"https://{opt.Tenant}.b2clogin.com/{opt.Tenant}.onmicrosoft.com/{opt.SigninPolicyId}/v2.0/";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = true,
ValidAudience = opt.GatewayApplication.ClientId,
};
options.Events = new JwtBearerEvents
{
OnTokenValidated = async context =>
{
// Get the users objectId:
var objectId = context.Principal.GetObjectId();
// TODO: Load user claims here:
var appIdentity = new ClaimsIdentity();
context.Principal.AddIdentity(appIdentity);
},
OnAuthenticationFailed = context =>
{
context.NoResult();
return Task.CompletedTask;
},
};
})
.AddJwtBearer("B2C_1A_BRS_SigninSignup", options =>
{
options.Authority = $"https://{opt.Tenant}.b2clogin.com/{opt.Tenant}.onmicrosoft.com/{opt.BrsInvitationPolicyId}/v2.0/";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = true,
ValidAudience = opt.GatewayApplication.ClientId,
};
options.Events = new JwtBearerEvents
{
OnTokenValidated = async context =>
{
// Get the users objectId:
var objectId = context.Principal.GetObjectId();
// TODO: Load user claims here:
var appIdentity = new ClaimsIdentity();
context.Principal.AddIdentity(appIdentity);
},
OnAuthenticationFailed = context =>
{
context.NoResult();
return Task.CompletedTask;
},
};
});
services.AddAuthorization(options =>
{
options.DefaultPolicy = new AuthorizationPolicyBuilder("B2C_1A_BRS_SigninSignup", "B2C_1A_Signin")
.RequireAuthenticatedUser()
.Build();
options.AddPolicy(PolicyNames.UserAccessPolicy, policy =>
{
policy.RequireAuthenticatedUser();
policy.Requirements.Add(new UserAccessRequirement());
policy.Requirements.Add(new ScopesRequirement(Constants.Scopes.BrokerServiceApiAccess));
});
});