Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can retrieve all users belonging to a particular role using ASP.NET Identity by using the UserManager and RoleManager classes. Here's an example:

  1. Get the role ID of the role you are interested in:
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
var roleId = roleManager.FindByName("MyRole").Id;
  1. Use the UserManager to retrieve all users that belong to the role:
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var users = userManager.Users.Where(u => u.Roles.Any(r => r.RoleId == roleId)).ToList();

The Users property of the userManager object will give you all the users in the database. The Where method filters out only those users who have a role with the given role ID. The ToList method converts the filtered users into a list.