Steve Moro

Steve Moro

  • NA
  • 16
  • 586

ASP.NET CORE 2.0 AppRole Management

Apr 22 2018 7:35 PM
Is there any View Model and CSHTML example of how to manage Add/Edit/Delete Roles from a User.
 
I have read that you can programatically register user and add Roles using
  1. public static async Task CreateUserRoles(IServiceProvider serviceProvider)  
  2. {  
  3. var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();  
  4. var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();  
  5. IdentityResult roleResult;  
  6. //Adding Addmin Role  
  7. var roleCheck = await RoleManager.RoleExistsAsync("Admin");  
  8. if (!roleCheck)  
  9. {  
  10. //create the roles and seed them to the database  
  11. roleResult = await RoleManager.CreateAsync(new IdentityRole("Admin"));  
  12. }  
  13. roleCheck = await RoleManager.RoleExistsAsync("Sales");  
  14. if (!roleCheck)  
  15. {  
  16. //create the roles and seed them to the database  
  17. roleResult = await RoleManager.CreateAsync(new IdentityRole("Sales"));  
  18. }  
  19. roleCheck = await RoleManager.RoleExistsAsync("Customer");  
  20. if (!roleCheck)  
  21. {  
  22. //create the roles and seed them to the database  
  23. roleResult = await RoleManager.CreateAsync(new IdentityRole("Customer"));  
  24. }  
  25. //Assign Admin role to the main User here we have given our newly loregistered login id for Admin management  
  26. ApplicationUser user = await UserManager.FindByEmailAsync("[email protected]");  
  27. var User = new ApplicationUser();  
  28. await UserManager.AddToRoleAsync(user, "Admin");  
  29. }  
However when the application is running I need an application admin to administer Application Roles with users.