Add User to Role ASP.NET Identity

后端 未结 7 711
梦毁少年i
梦毁少年i 2020-12-07 16:47

I know the new Membership includes a \"Simple Role Provider.\"

I can\'t find any help related to creating a user and assigning a role when the user is created. I\'v

7条回答
  •  星月不相逢
    2020-12-07 17:22

    While I agree with the other answers regarding the RoleManager, I would advice to examine the possibility to implement Authorization through Claims (Expressing Roles as Claims).

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

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

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

    So instead of using the RoleManager, you can "add a user to a role" from the UserManager, doing something like this:

    var um = new UserManager();
    um.AddClaimAsync(1, new Claim("http://schemas.microsoft.com/ws/2008/06/identity/claims/role", "administrator"));
    

    With the above lines you have added a role claim with the value "administrator" to the user with the id "1"...

    Claims authorization, as suggested by MSFT, can simplify and increase the performance of authentication and authorization processes eliminating some back-end queries every time authorization takes place.

    Using Claims you may not need the RoleStore anymore. (AspNetRoles, AspNetUserRoles)

提交回复
热议问题