I can get all users like this
var users = UserManager.Users.ToList();
I can find a role like this
var role = db.Roles.SingleOrDefault(m => m.Name == "User");
I want to list all users with the role name "User" in the AspNetRoles table (I have User
and Admin
).
var role = db.Roles.SingleOrDefault(m => m.Name == "User");
var usersInRole = db.Users.Where(m => m.Roles.Any(r => r.RoleId == role.Id)).ToList();
return View(usersInRole);
I get the role in var role
but userInRole
gives me Count = 0
when I debug. No compile errors.
You can use bellow code for retrieve all users in specific role (Identity 2.x.x):
var users = await _userManager.GetUsersInRoleAsync("RoleName");
or for synchronize usage:
var users = _userManager.GetUsersInRoleAsync("RoleName").Result;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With