-1

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.

jps
  • 20,041
  • 15
  • 75
  • 79
Radziu
  • 29
  • 3

1 Answers1

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);
    }
}
jps
  • 20,041
  • 15
  • 75
  • 79
A-A-ron
  • 529
  • 1
  • 5
  • 14