I was looking everywhere but all the answers that I found are not working. How to list all roles on Registry page? I'm working with ASP.NET Core 2.2.
Asked
Active
Viewed 61 times
-1
-
Possible duplicate of [asp.net identity get all roles of logged in user](https://stackoverflow.com/questions/21688928/asp-net-identity-get-all-roles-of-logged-in-user) – Masoud Andalibi Apr 15 '19 at 12:18
-
Use the `RoleManager` to get the `Roles`. – Haytam Apr 15 '19 at 12:32
1 Answers
0
This can be accomplished fairly easily as long as you are using the existing DI container that will allow you to access available roles. Below is just an example of how this could be set up in the controller.
[Authorize(Roles = "Administrator")]
public class RolesController : BaseController
{
public RolesController(ApplicationDbContext context, UserManager<IdentityUser> userManager, RoleManager<IdentityRole> roleManager) : base(context, userManager, roleManager)
{
}
[HttpGet]
public async Task<IActionResult> Index()
{
List<IdentityRole> roles = await RoleManager.Roles.ToListAsync();
return View(roles);
}
}