更新时间:2023-12-01 07:50:09
使用基于策略的身份验证.在这里,您可以检查当前的 ClaimsPrincipal
( context.User
)是否具有2个 Identities
,每个成功通过的身份验证方案中都有1个.配置政策
Use policy based authentication. There you can check if current ClaimsPrincipal
(context.User
) has 2 Identities
, 1 from each successfully passed authentication scheme. Configure policy
services.AddAuthorization(options =>
{
options.AddPolicy("RequireAllSchemes", policy =>
{
policy.AddAuthenticationSchemes(CookieAuthenticationDefaults.AuthenticationScheme);
policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme);
policy.RequireAuthenticatedUser();
policy.RequireAssertion(context =>
{
return context.User.Identities.Count() == 2;
});
});
});
指定控制器的授权策略
[Authorize(Policy = "RequireAllSchemes")]
public class MixedController : Controller