如何从Asp.Net核心身份验证中获取策略列表?

Gio*_*rgo 8 asp.net authentication authorization

我正在使用Asp.Net Core 1.1.1及其授权/认证系统创建一个Web应用程序.

在我的startup.cs文件中,我已经配置了我需要的各种策略:

public void ConfigureServices(IServiceCollection services)
{
        servizi.AddAuthorization(options =>
        {
            options.AddPolicy("Insert", policyBuilder => policyBuilder .RequireClaim("AllowInsert"));
            options.AddPolicy("Update", policyBuilder => policyBuilder .RequireClaim("AllowUpdate"));
            options.AddPolicy("Delete", policyBuilder => policyBuilder .RequireClaim("AllowDelete"));
        });
}
Run Code Online (Sandbox Code Playgroud)

然后在控制器中,我已将声明分配给角色(管理员角色):

    public async Task<IActionResult> AssignClaimstoAdminRole()
    {
        await _roleManager.AddClaimAsync(await _roleManager.FindByNameAsync("Administrator"), new Claim("AllowInsert", "true"));
        await _roleManager.AddClaimAsync(await _roleManager.FindByNameAsync("Administrator"), new Claim("AllowUpdate", "true"));
        await _roleManager.AddClaimAsync(await _roleManager.FindByNameAsync("Administrator"), new Claim("AllowDelete", "true"));

        await _loginManager.RefreshSignInAsync(await _userManager.FindByNameAsync(User.Identity.Name));
        return RedirectToAction("SomeAction", "MyController");
    }
Run Code Online (Sandbox Code Playgroud)

最后我保护了我注入授权服务的观点:

@using Microsoft.AspNetCore.Authorization
@inject IAuthorizationService AuthorizationService

@if (await AuthorizationService.AuthorizeAsync(User, "Insert"))
{
     <i role="submit" class="fa fa-floppy-o fa-2x"></i>
}
Run Code Online (Sandbox Code Playgroud)

我的问题是: 我想检索在startup.cs中创建的策略列表,这样我最终可以构建一个页面来添加其他用户/角色,为它们分配一个或多个已存在于系统中的策略.

是否有一个对象暴露了以前添加的策略的集合?提前致谢!

bar*_*ost 2

更好地依赖索赔。您始终可以访问索赔列表。

我目前正在研究这个问题,其中应用程序的控制器使用策略来授予访问权限,而我的菜单构建器使用声明来添加或跳过菜单项。

我认为我可以使用相同的字符串作为索赔值和保单名称,从而更易于管理。

我也发现了一篇有趣的文章,关于复杂的过滤、策略和声明:https://andrewlock.net/custom-authorization-policies-and-requirements-in-asp-net-core/