9

I just updated to ASP.NET Identity EntityFramework 2.0.0-beta1 and got a compilation errors for my Roles classes. Maybe somebody can give me some clue how to get all users for a specific Role?

It's something very close, when I browse with Intellisense I am almost there, but need a tip :-).

This is how it worked before update:

 user.Roles.First().Role.Name.Equals("Admins")
Svitlana
  • 2,324
  • 4
  • 22
  • 31
  • What error are you getting? It looks like you are trying to access `IsInRole("Admins")`. If you must do this through EF, consider using `user.Roles.Any(r => r.Name == "Admins")`. – Jim Wooley Mar 05 '14 at 21:10

3 Answers3

11

Its only exposed on the EF implementation layer so:

roleManager.FindByName("Admins").Users
Hao Kung
  • 28,040
  • 6
  • 84
  • 93
  • 3
    Thank you, Mr. Kung! And Is there a more elegant way to get Admins from IdentityUserRole Table? `code(var userIds = roleManager.FindByName("Admins").Users.Select(e => e.UserId).ToList();) ` var admins = userManager.Users.Where(e => userIds.Contains(e.Id)); – Svitlana Mar 06 '14 at 19:38
  • Svitlana, did you find a more elegant way? This seems like it should be an easy thing to do, but this is ugly. – ThisGuy May 23 '14 at 01:45
  • I have a question. Why does the result of this only exposes the IdentityUserRoles properties, not of users? How do I see the User properties instead of the navigation table? – christianleroy May 19 '16 at 05:54
9

The accepted answer returns CustomUserRoles. If you are looking for the list of ApplicationUsers, try:

public IList<ApplicationUser> GetApplicationUsersInRole(string roleName)
{
    var selectedUserIds = from role in roleManager.Roles
                          where role.Name == roleName
                          from user in role.Users
                          select user.UserId;
    // this _users comes from the ApplicationDbContext.ApplicationUser
    return _users.Where(applicationUser => selectedUserIds.Contains(applicationUser.Id)).ToList();
}
VahidN
  • 18,457
  • 8
  • 73
  • 117
  • :-).The only comment - I AM SO GLAD I SWITCHED TO ANGULAR/FIREBASE and do not need to carry that burden.... But thanks for your time, amigo. – Svitlana Feb 14 '15 at 16:23
8

I really liked VahidN's solution, but I modified it a bit. I made it into one query that uses the dbcontext. This allows you to add additional clauses into the query (e.g. deleted, emailconfirmed, etc.)

public IEnumerable<ApplicationUser> GetApplicationUsersInRole(string roleName)
{
      return from role in context.Roles
             where role.Name == roleName
             from userRoles in role.Users
             join user in context.Users
             on userRoles.UserId equals user.Id
             where user.EmailConfirmed == true
               && user.IsDeleted == false
             select user;
 }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Rogala
  • 2,679
  • 25
  • 27
  • I think even more streamlined is getting the role first, `var role = roleManager.FindByName(roleName);` and then querying, `var _usersInRole = role.Users.Select(j => context.Users.Where(k=> k.Id == j.UserId));` - That doesn't look as overwhelming as your query, but also, you can add additional queries to this as you did. – EluciusFTW May 07 '15 at 09:37