AuthorizeFilter 添加 AuthorizationPolicy 的代码如下:
services.AddMvc(o =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireRole(ROLE_NAME)
.Build();
o.Filters.Add(new AuthorizeFilter(policy));
});
请问如何通过 IAuthorizationPolicyProvider 接口获取上面添加的 AuthorizationPolicy ?
AuthorizeFilter 添加的 AuthorizationPolicy 是无法通过 IAuthorizationPolicyProvider 获取到的,需要在 services.AddAuthorization 中通过 AddPolicy 添加
services.AddAuthorization(options =>
{
options.AddPolicy(POLICY_NAME, policy =>
{
policy.RequireAuthenticatedUser().RequireRole(ROLE_NAME);
});
});