Asp.Net core MVC6 How to initially add roles in Identity 3

后端 未结 2 560
说谎
说谎 2020-12-31 20:58

I\'ve looked for this in Stackoverflow and so far it appears there are plenty of questions on adding roles in Identity 1 & 2 but its different in Identity 3.

I w

2条回答
  •  耶瑟儿~
    2020-12-31 21:38

    EDIT

    Identity

    In Identity RoleManager is for creating roles and UserManager is for adding users to roles. This is an example to point you in the right direction. The code below is for creating a new role Administrator

    if (!roleManager.RoleExists("Administrator"))
                {
                    MyIdentityRole newRole = new MyIdentityRole("Administrator", "Administrators can do something with data");
                    roleManager.Create(newRole);
                }  
    

    EDIT

    Further, this is for adding a user to a role and this also an example:

     \\assuming you test if the user has been assigned to the role "Administrator" before adding them to that role
    
     if(RoleAdministrator == true){
               userManager.AddToRole(User.Id, "Administrator");
           }
    

提交回复
热议问题