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:
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
var roleId = roleManager.FindByName("MyRole").Id;
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.