Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all users with a specific role in ASP.NET Core 2.0

I'm just getting started with porting a application from ASP.NET MVC 5 to ASP.NET Core 2.0 (with EF Core and Identity). All has been going smooth, but I've ran into a problem that I can't seem to find an answer to.

What I want to do is simply to fetch all users in a database, based on the role they are assigned (Admin, User, etc.).

This was simple in MVC 5 where I was just using:

_db.UserCompany.Where(x => x.User.Roles.Any(s => s.RoleId == adminRole.Id)

However, with .NET Core / Entity Framework Core I don't seem to be able to navigate through the User -> Role anymore.

So my question is if there is a already implemented way in Identity/EF Core to achieve what I want? Or is it necessary to implement a custom solution?

Thanks in advance!

like image 951
Subtractive Avatar asked Sep 23 '17 22:09

Subtractive


1 Answers

For me, User Manager was the answer

private readonly UserManager<ApplicationUser> _userManager;

_userManager.GetUsersInRoleAsync("myRole").Result
like image 72
MateusBello Avatar answered Sep 21 '22 07:09

MateusBello