Marius Vasile

Marius Vasile

  • 594
  • 1.9k
  • 145.7k

.NET Core Razor- New Roles assigned after registration

Jun 7 2020 2:18 AM
I can assign a role at Registration level e.g Visitor, but how do I manage to change that role from User/Edit page? Also, how to add all the roles required to aspnetrole and then assign to users as needed? 
Model 
  1. public class UserDataClass : IdentityUser  
  2.     {  
  3.         public string UserOrganization { getset; }  
  4.         public string UserStatus { getset; }  
  5.         public UserRoleList URole { getset; }  
  6.         public ICollection<AppUserRole> UserRoles { getset; }  
  7.   
  8.     }  
  9.   
  10.     public class UserRoleClass : IdentityRole  
  11.     {  
  12.         public string UsRole { getset; }  
  13.     }  
  14.   
  15.     public class AppUserRole : IdentityUserRole<string>  
  16.     {  
  17.         public virtual UserDataClass UsName { getset; }  
  18.         public virtual UserRoleClass UsRole { getset; }  
  19.     }  
  20.   
  21.     public enum UserRoleList  
  22.     {  
  23.         Admin = 0,  
  24.         UserAdmin = 1,  
  25.         Management = 2,  
  26.         Visitor = 3  
  27.     }  
 Registration - assign Visitor (working)
 
  1. if (!await _roleManager.RoleExistsAsync("Visitor"))  
  2.                     {  
  3.                         var role = new IdentityRole("Visitor");  
  4.                         var res = await _roleManager.CreateAsync(role);  
  5.                         if (res.Succeeded)  
  6.                         {  
  7.                             await _userManager.AddToRoleAsync(user, "Visitor");  
  8.                             await _signInManager.SignInAsync(user, isPersistent: false);  
  9.                             _logger.LogInformation("A new account was created");  
  10.                         }  
  11.                     }  
startup
 
  1. services.AddIdentity<UserDataClass, UserRoleClass>(options => options.SignIn.RequireConfirmedAccount = true)  
  2.                 .AddDefaultUI()  
  3.                 .AddRoles<UserRoleClass>()  
 
 
 
 

Answers (6)