How to get all users in Role (Microsoft ASP.NET Identity EntityFramework 2.0.0-beta1)?

asp.net asp.net-identity entity-framework

Question

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")
1
9
3/5/2014 8:59:15 PM

Accepted Answer

Its only exposed on the EF implementation layer so:

roleManager.FindByName("Admins").Users
10
3/6/2014 12:36:26 AM

Popular Answer

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();
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow