Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add role in ASP.NET Identity

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 documentation on this issue.

like image 396
daniel Avatar asked Oct 23 '13 12:10

daniel


2 Answers

RoleManager = new RoleManager<IdentityRole>(
                  new RoleStore<IdentityRole>(new MyDbContext()));
var roleresult = RoleManager.Create(new IdentityRole(roleName));
like image 110
graycrow Avatar answered Oct 28 '22 07:10

graycrow


Starting with the .NET Framework 4.5, Windows Identity Foundation (WIF) has been fully integrated into the .NET Framework.

I would advice to examine the possibility, in my opinion the preferred, to implement Authorization through Claims (Expressing Roles as Claims).

When the IsInRole() method is called, there is a check made to see if the current user has that role. In claims-aware applications, the role is expressed by a role claim type that should be available in the token.

The role claim type is expressed using the following URI: "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"

So from the UserManager you can do something like this (without the RoleManager):

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

Claims can simplify and increase the performance of authentication and authorization processes. You can use the roles stored as claims to eliminate back-end queries every time authorization takes place.

Using Claims you will not need the RoleStore anymore (at least for the equivalent authorization purposes...)

like image 34
MyOwnWay Avatar answered Oct 28 '22 08:10

MyOwnWay