ASP.NET Core:如果通过IAuthorizationPolicyProvider获取Aut

2019-01-02 23:53发布

AuthorizeFilter 添加 AuthorizationPolicy 的代码如下:

services.AddMvc(o =>
    {
        var policy = new AuthorizationPolicyBuilder()
            .RequireRole(ROLE_NAME)
            .Build();
        o.Filters.Add(new AuthorizeFilter(policy));
    });

请问如何通过 IAuthorizationPolicyProvider 接口获取上面添加的 AuthorizationPolicy ?

1条回答
Lonely孤独者°
2楼-- · 2019-01-03 00:28

AuthorizeFilter 添加的 AuthorizationPolicy 是无法通过 IAuthorizationPolicyProvider 获取到的,需要在 services.AddAuthorization 中通过 AddPolicy 添加

services.AddAuthorization(options =>
{
    options.AddPolicy(POLICY_NAME, policy =>
    {
        policy.RequireAuthenticatedUser().RequireRole(ROLE_NAME);
    });
});
查看更多
登录 后发表回答