ASP.NET Authentication: Get All Users in a Role

How to get a list of all users in a role? Before this was possible using Roles.GetUsersInRole, but with the new Identity I can not find anything like it.

+7
c # asp.net-mvc-5 asp.net-identity
source share
5 answers

Impossible through RoleManager in RTM version 1.0, in 1.1 it will be displayed through IQueryable RoleManager.Roles. For 1.0 you need to go down to the implementation level (i.e. Context db)

+5
source share

I have not seen the built-in method, but it is quite easy to implement. I have this method in a UserManager application specific to my application:

public IQueryable<User> GetUsersInRole(string roleName) { return from user in Users where user.Roles.Any(r => r.Role.Name == roleName) select user; } 

The SQL output was reasonable:

 SELECT [Extent1].[Id] AS [Id], [Extent1].[Email] AS [Email], [Extent1].[EmailConfirmed] AS [EmailConfirmed], [Extent1].[PasswordHash] AS [PasswordHash], [Extent1].[SecurityStamp] AS [SecurityStamp], [Extent1].[PhoneNumber] AS [PhoneNumber], [Extent1].[PhoneNumberConfirmed] AS [PhoneNumberConfirmed], [Extent1].[TwoFactorEnabled] AS [TwoFactorEnabled], [Extent1].[LockoutEndDateUtc] AS [LockoutEndDateUtc], [Extent1].[LockoutEnabled] AS [LockoutEnabled], [Extent1].[AccessFailedCount] AS [AccessFailedCount], [Extent1].[UserName] AS [UserName] FROM [dbo].[AspNetUsers] AS [Extent1] WHERE EXISTS (SELECT 1 AS [C1] FROM [dbo].[AspNetUserRoles] AS [Extent2] INNER JOIN [dbo].[AspNetRoles] AS [Extent3] ON [Extent2].[RoleId] = [Extent3].[Id] WHERE ([Extent1].[Id] = [Extent2].[UserId]) AND ([Extent3].[Name] = @p__linq__0) ) 
+10
source share

For some reason, the very good request suggested by @ChoptimusPrime above did not compile for me in ASP.NET Identity 2.2.1. I wrote an advanced function:

 public static IQueryable<User> GetUsersInRole(DbContext db, string roleName) { if (db != null && roleName != null) { var roles = db.Roles.Where(r => r.Name == roleName); if (roles.Any()) { var roleId = roles.First().Id; return from user in db.Users where user.Roles.Any(r => r.RoleId == roleId) select user; } } return null; } 
+4
source share

You can use the Entity Framework, but with Asp.Net Identity 1.0 is not yet possible. You must wait for the release of Identity 2.0.

 using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString)) { string queryString = "SELECT AspNetUsers.UserName FROM dbo.AspNetUsers INNER JOIN dbo.AspNetUserRoles ON " + "AspNetUsers.Id=AspNetUserRoles.UserId WHERE AspNetUserRoles.RoleID='" + id + "'"; SqlCommand command = new SqlCommand(queryString, connection); command.Connection.Open(); List<string> @out = null; dynamic reader = command.ExecuteReader(); while (reader.Read()) { if (@out == null) @out = new List<string>(); @out.Add(reader.GetString(0)); } return @out; } 
0
source share

This is for the new MVC 5 ASP.NET Identity :

 var managerRole = TMRoles.GetIdentityRole(TMRoles.Manager); var managers = managerRole.Users; public class TMRoles { private static RoleManager<IdentityRole> RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new TMContext())); public static string Manager { get { return "Manager"; } } public static IdentityRole GetIdentityRole(string roleName) { return RoleManager.FindByName(roleName); } } 
0
source share

All Articles