向Role添加声明

时间:2017-06-24 19:14:43

标签: asp.net-identity roles claims-based-identity claims

我想在项目中为每个角色添加声明,然后验证权限,而不是整个角色。 我已阅读文章http://benfoster.io/blog/asp-net-identity-role-claims并看到以下几行:

await roleManager.AddClaimAsync(adminRole, new Claim(CustomClaimTypes.Permission, "projects.view"));
await roleManager.AddClaimAsync(adminRole, new Claim(CustomClaimTypes.Permission, "projects.create"));
await roleManager.AddClaimAsync(adminRole, new Claim(CustomClaimTypes.Permission, "projects.update"));

我创建了以下角色管理器:

public class ApplicationRoleManager : RoleManager<IdentityRole>
{
    public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore)
        : base(roleStore)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
    }
}

并在启动时注册:

app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);

然后得到它:

    public ApplicationRoleManager RoleManager
    {
        get
        {
            return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
        }
        private set
        {
            _roleManager = value;
        }
    }

但是RoleManager没有AddClaimAsync方法。如何向角色添加声明?

0 个答案:

没有答案