获取所有在ASP.NET Core 2.0中具有特定角色的用户 [英] Get all users with a specific role in ASP.NET Core 2.0
问题描述
我刚刚开始将应用程序从ASP.NET MVC 5移植到ASP.NET Core 2.0(具有EF Core和Identity).一切都进行得很顺利,但是我遇到了一个我似乎找不到答案的问题.
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.).
在我刚刚使用的MVC 5中,这很简单:
This was simple in MVC 5 where I was just using:
_db.UserCompany.Where(x => x.User.Roles.Any(s => s.RoleId == adminRole.Id)
但是,使用.NET Core/Entity Framework Core,我似乎无法再导航到User-> Role.
However, with .NET Core / Entity Framework Core I don't seem to be able to navigate through the User -> Role anymore.
所以我的问题是,Identity/EF Core中是否已经存在一种实现我想要的方法?还是有必要实施自定义解决方案?
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?
提前谢谢!
推荐答案
对我来说,用户管理器是答案
For me, User Manager was the answer
private readonly UserManager<ApplicationUser> _userManager;
_userManager.GetUsersInRoleAsync("myRole").Result
这篇关于获取所有在ASP.NET Core 2.0中具有特定角色的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!