Add role in ASP.NET Identity

前端 未结 4 1854
傲寒
傲寒 2020-12-07 17:03

How can I add a Role in the new ASP.NET Identity system (1.0)? There is a UserStore class but no RoleStore class.

I can\'t find any documen

4条回答
  •  日久生厌
    2020-12-07 17:11

    ASP.NET identity is claims aware with respect to roles. That really confused me because in the previous system you configured membership and role providers in web.config.

    The issue for me is that I have code like this:

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

    Fortunately, this logic still works. You can see the logic in the CreateAsync function in ClaimsIdentityFactory.cs which is in Microsoft.AspNet.Identity.Core. One of the arguments is UserManager. It asks it if it SupportsUserRole and if so then it calls GetRolesAsync and adds each role as a claim to the ClaimIdentity. There is no need to do this yourself.

    IsInRole uses claims as described here:

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

提交回复
热议问题