Base solution for your next web application
Open Closed

Get List of user in a specif Role #1855


User avatar
0
andmattia created

Hi

I try to get the list of user in a role with this code but not work

var roleGlobal = await _roleManager.Roles.FirstOrDefaultAsync(t => t.Name == StaticRoleNames.Tenants.Manager);
            var users = await UserManager.Users.Where(
                t => t.Roles.All(q => q.RoleId == roleGlobal.Id && q.UserId == t.Id)).ToListAsync();

2 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    We did this in AspNet Zero (<a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/dev/src/MyCompanyName.AbpZeroTemplate.Application/Authorization/Users/UserAppService.cs#L62">https://github.com/aspnetzero/aspnet-ze ... ice.cs#L62</a>)

    Also, your code seems wrong, this may work:

    var roleGlobal = await _roleManager.Roles.FirstOrDefaultAsync(t => t.Name == StaticRoleNames.Tenants.Manager);
    var users = await UserManager.Users.Where(u => u.Roles.Any(r => r.RoleId == roleGlobal.Id && r.UserId == u.Id)).ToListAsync();
    

    But I didn't try it. It's better to write some join as we did in aspnet zero.

  • User Avatar
    0
    andmattia created

    Ok

    I test and I send my feedback, also check if I can change to join in LINQ