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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-08 01:52:38 +0000
Seen: 14 times
Last updated: Jun 08 '23
How to change the default table names for identity in an override manner?
Does Asp .Net Core Identity log out automatically after a certain period of time?
Is it hard to generate an Identity matrix in Numpy by using the inverse of multiple matrices of A?
How can a role assignment be added to a managed identity that is assigned to a system in Azure?
What is the meaning of the role being returned as 'undefined' in DiscordJS?
Is it possible to automatically assign a role to a new member upon joining in Wix?
What is the method to enable users to provide file inputs in React Native?
What is the reason for websites adjusting the default font size to 14px?