Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of users with assigned roles in asp.net identity 2.0

I have a drop down list box which lists roles. I want to get the list of users having that role. I mean list of users that are in "Administrator" role or "CanEdit" role. Here is my code:

public IQueryable<Microsoft.AspNet.Identity.EntityFramework.IdentityUser>    GetRolesToUsers([Control] string ddlRole) {       //ddlRole returns role Id, based on this Id I want to list users    var _db = new ApplicationDbContext();   IQueryable<Microsoft.AspNet.Identity.EntityFramework.IdentityUser> query = _db.Users;    if (ddlRole != null)   {     //query = query.Where(e => e.Claims == ddlRole.Value);  ???????                 }    return query; } 

Please help.

Updated Code (still error)

public List<IdentityUserRole> GetRolesToUsers([Control]string ddlRole) {    var roleManager =     new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));   var users = roleManager.FindByName("Administrator").Users.ToList();   return users; } 

Error: The Select Method must return one of "IQueryable" or "IEnumerable" or "Microsoft.AspNet.Identity.EntityFramework.IdentityUser" when ItemType is set to "Microsoft.AspNet.Identity.EntityFramework.IdentityUser".

I tried various castings but none of them helped.

UPDATE (working solution)

Thanks to chris544, his idea helped me to fix this. Here is working method:-

public List<ApplicationUser> GetRolesToUsers([Control]string ddlRole) {   var context = new ApplicationDbContext();   var users =     context.Users.Where(x => x.Roles.Select(y => y.RoleId).Contains(ddlRole)).ToList();    return users; } 
like image 732
Abhimanyu Avatar asked May 23 '14 08:05

Abhimanyu


People also ask

How do you get user role in identity?

it gives you the AspNetUserInRoles which stores UserId and RoleId. Instead you could try UserManger 's GetRoles method which will return you List<string> of roles user is assigned. But as you mentioned it will be only one role hence you can take first value from the result of GetRoles method.

Which method enables you to return a list of users in a role that has a particular username?

The following code example uses the GetUsersInRole method to get a list of the users in a particular role and binds the results to a GridView control.

How do you add roles to AspNetRoles?

Right click “AspNetRoles” table and click New Query. Step 5: Insert roles in “AspNetRoles” table, using SQL query. The screenshots, given below, explain how to insert the roles in the specified table. Step 6: We need to map with role ID and users ID now.


2 Answers

Not an expert, but ...

There seemed to be no built in funcionality for this in Identity and I could not get it work from built in Roles also (it seems to not work with claims based Identity).

So I ended up doing something like this:

var users = context.Users             .Where(x => x.Roles.Select(y => y.Id).Contains(roleId))     .ToList(); 
  • x.Roles.Select(y => y.Id) gets a list of all role ids for user x
  • .Contains(roleId) checks if this list of ids contains necessary roleId
like image 106
chris544 Avatar answered Sep 23 '22 04:09

chris544


I find the role by the role name input. After, I find list users by id of the role.

public List<ApplicationUser> GetUsersInRole(string roleName) {  var roleManager =    new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new  ApplicationDbContext()));  var role = roleManager.FindByName(roleName).Users.First();  var usersInRole =    Users.Where(u => u.Roles.Select(r => r.RoleId).Contains(role.RoleId)).ToList();  return usersInRole; } 
like image 28
Trieu Doan Avatar answered Sep 22 '22 04:09

Trieu Doan