在ASP.NET标识中添加角色

时间:2013-10-23 12:01:20

标签: asp.net asp.net-identity

如何在新的ASP.NET身份系统(1.0)中添加角色? 有一个UserStore类,但没有RoleStore类。

我找不到关于这个问题的任何文件。

4 个答案:

答案 0 :(得分:42)

RoleManager = new RoleManager<IdentityRole>(
                  new RoleStore<IdentityRole>(new MyDbContext()));
var roleresult = RoleManager.Create(new IdentityRole(roleName));

答案 1 :(得分:34)

从.NET Framework 4.5开始,Windows Identity Foundation(WIF)已完全集成到.NET Framework中。

我建议检查在我看来是首选的,通过声明实施授权的可能性(Expressing Roles as Claims)。

调用IsInRole()方法时,会检查当前用户是否具有该角色。 在声明感知应用程序中,角色由应在令牌中提供的角色声明类型表示。

角色声明类型使用以下URI表示: “http://schemas.microsoft.com/ws/2008/06/identity/claims/role

所以从 UserManager 你可以做这样的事情(没有RoleManager):

var um = new UserManager();
um.AddClaimAsync(1, new Claim(ClaimTypes.Role, "administrator"));

声明可以简化并提高身份验证和授权过程的性能。 您可以使用存储为声明的角色在每次授权时消除后端查询。

使用声明您不再需要RoleStore(至少为了等效的授权目的......)

答案 2 :(得分:6)

我在一个示例asp.net网页 page_load 中使用了以下代码段,以便开始掌握ASP身份识别的工作方式

   UserManager userManager = new UserManager();
    var roleStore = new RoleStore<IdentityRole>(new ApplicationDbContext());
    var roleManager = new RoleManager<IdentityRole>(roleStore);
    var applicationRoleAdministrator = new IdentityRole("superadmin");
    if (!roleManager.RoleExists(applicationRoleAdministrator.Name))
    {
        roleManager.Create(applicationRoleAdministrator);
    }
     ApplicationUser applicationUserAdministrator = userManager.FindByName(User.Identity.Name);

    if (!userManager.GetRoles(applicationUserAdministrator.Id).Contains("superadmin"))
    {
        Response.Redirect("~/account/login.aspx?ReturnUrl=" + Request.Url.AbsolutePath);
    }

当然,ApplicationDbContext下面是使用ASP.NET 4.5+模板自动生成的,如下所示

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }
    }

同样创建应用程序角色管理器类

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>()));
        return new ApplicationRoleManager(new RoleStore<IdentityRole>(new ApplicationDbContext()));
    }
}

还在您的startup.Auth.cs =&gt;中添加以下行。 ConfigureAuth(IAppBuilder app)方法

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

然后在你的控制器中:

private ApplicationRoleManager _roleManager;

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

我是这个Identity Stuff的新手,我不确定是否有必要,或者我干净利落,但这些步骤对我有用

答案 3 :(得分:2)

ASP.NET身份是关于角色的声明。这让我很困惑,因为在之前的系统中,您在web.config中配置了成员资格和角色提供程序。

我的问题是我有这样的代码:

HttpContext.Current.User.IsInRole("some role")

幸运的是,这种逻辑仍然有效。您可以在CreateAsync中的ClaimsIdentityFactory.cs中查看Microsoft.AspNet.Identity.Core函数中的逻辑。其中一个论点是UserManager。它询问是SupportsUserRole,如果是,则调用GetRolesAsync并将每个角色作为声明添加到ClaimIdentity。没有必要自己这样做。

IsInRole使用此处所述的声明:

http://msdn.microsoft.com/en-us/library/hh545448.aspx

相关问题