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
- public static async Task CreateUserRoles(IServiceProvider serviceProvider)
- {
- var RoleManager = serviceProvider.GetRequiredService
>(); - var UserManager = serviceProvider.GetRequiredService
>(); - IdentityResult roleResult;
- //Adding Addmin Role
- var roleCheck = await RoleManager.RoleExistsAsync("Admin");
- if (!roleCheck)
- {
- //create the roles and seed them to the database
- roleResult = await RoleManager.CreateAsync(new IdentityRole("Admin"));
- }
- roleCheck = await RoleManager.RoleExistsAsync("Sales");
- if (!roleCheck)
- {
- //create the roles and seed them to the database
- roleResult = await RoleManager.CreateAsync(new IdentityRole("Sales"));
- }
- roleCheck = await RoleManager.RoleExistsAsync("Customer");
- if (!roleCheck)
- {
- //create the roles and seed them to the database
- roleResult = await RoleManager.CreateAsync(new IdentityRole("Customer"));
- }
- //Assign Admin role to the main User here we have given our newly loregistered login id for Admin management
- ApplicationUser user = await UserManager.FindByEmailAsync("[email protected]");
- var User = new ApplicationUser();
- await UserManager.AddToRoleAsync(user, "Admin");
- }
However when the application is running I need an application admin to administer Application Roles with users.
Replies
Know the answer? Post it — somebody with the same question will find it here.