Asp.net core 如何统计ASP.NET Core 2.0中具有特定角色的所有用户
我正在使用Identity进行Asp.net core 2.0项目。我的项目是用Core1.1创建和开发的,一段时间后迁移到Core2.0。 在迁移到Core2.0之后,一切都很好,但在项目的一部分中,我需要让所有用户都扮演一个特定的角色。 在Asp.Net Core 1.1中,我使用以下代码获得所有特定角色的计数:Asp.net core 如何统计ASP.NET Core 2.0中具有特定角色的所有用户,asp.net-core,asp.net-core-2.0,identity,Asp.net Core,Asp.net Core 2.0,Identity,我正在使用Identity进行Asp.net core 2.0项目。我的项目是用Core1.1创建和开发的,一段时间后迁移到Core2.0。 在迁移到Core2.0之后,一切都很好,但在项目的一部分中,我需要让所有用户都扮演一个特定的角色。 在Asp.Net Core 1.1中,我使用以下代码获得所有特定角色的计数: r.Users.Count 完成代码: public IActionResult Index() { List<ApplicationRol
r.Users.Count
完成代码:
public IActionResult Index()
{
List<ApplicationRoleViewModel> model = new List<ApplicationRoleViewModel>();
model = _roleManager.Roles.Select(r => new ApplicationRoleViewModel
{
Id = r.Id,
Name = r.Name,
Description = r.Description,
NumberOfUsers = r.Users.Count
}).ToList();
return View(model);
}
但是在Core2.0中没有用户
对象。
我也看到了这一点,但我无法解决我的问题。
现在,我如何统计ASP.NET Core 2.0中具有特定角色的所有用户?集合存储用户之间的所有引用以及用户所属的角色。您可以这样做:
var allUserRoles = _identityDb.UserRoles.ToList();
model = _roleManager.Roles.Select(r => new ApplicationRoleViewModel
{
Id = r.Id,
Name = r.Name,
Description = r.Description,
NumberOfUsers = allUserRoles.Count(ur => ur.RoleId == r.Id)
}).ToList();
请记住,这是非常低效的,因为它在您的操作中进行计数,而不是在数据库中。理想情况下,这将由生成的sql查询直接完成,但如果您没有处理大量的用户/角色,这个简单的解决方案应该可以正常工作
如果您已经有一个自定义的
IdentityRole
和UserRole
模型,您应该能够添加允许您使用IdentityDb.Roles.Include()根据RoleId引用UserRoles的属性。或者,您可以使用类似于下面的查询来执行自定义sql命令,该命令将返回所需的信息。请记住,您的标识数据库的列或表可能在此处有所不同
List<ApplicationRoleViewModel> vm = new List<ApplicationRoleViewModel>();
using (var dbConnection = _identityDb.Database.GetDbConnection())
using (var dbCommand = dbConnection.CreateCommand())
{
dbCommand.CommandText =
@"SELECT
r.Id,
r.Name,
r.Description
Count(ur.UserId) as NumberOfUsers
FROM AspNetRoles r
FULL OUTER JOIN AspNetUserRoles ur ON r.Id = ur.RoleId
GROUP BY r.Id, r.Name, r.Description";
dbConnection.OpenAsync().Wait();
using (var result = dbCommand.ExecuteReader())
{
while (result.Read())
{
vm.Add(new ApplicationRoleViewModel()
{
Id = result.GetString(0),
Name = result.GetString(1),
Description = result.GetString(2),
NumberOfUsers = result.GetInt32(3)
});
}
}
}
List vm=new List();
使用(var dbConnection=_identityDb.Database.GetDbConnection())
使用(var dbCommand=dbConnection.CreateCommand())
{
dbCommand.CommandText=
@“选择
r、 身份证,
r、 名字,
r、 描述
将(您的用户ID)计数为NumberOfUsers
来自Aspnetr
完全外部连接AspNetUserRoles ur ON r.Id=ur.RoleId
按r.Id、r.Name、r.Description分组”;
dbConnection.OpenAsync().Wait();
使用(var result=dbCommand.ExecuteReader())
{
while(result.Read())
{
添加(新的ApplicationRoleViewModel()
{
Id=result.GetString(0),
Name=result.GetString(1),
Description=result.GetString(2),
NumberOfUsers=result.GetInt32(3)
});
}
}
}
如果asp.net标识配置正确,则可以将UserManager和RoleManager插入控制器中
public class AccountController : Controller
{
private UserManager<IdentityUser> userManager;
private RoleManager<IdentityRole> roleManager;
public AccountController(UserManager<IdentityUser> userMgr,
RoleManager<IdentityRole> roleMgr)
{
userManager = userMgr;
roleManager = roleMgr;
}
}
公共类AccountController:控制器
{
私有用户管理器用户管理器;
私人角色经理;
公共帐户控制器(UserManager userMgr,
角色经理(roleMgr)
{
userManager=userMgr;
roleManager=roleMgr;
}
}
现在,您可以在userManager上调用GetUsersInRoleAsync(“myRole”),以在您的操作方法中获取myRole中的所有用户。是否可以解释一下更好的方法或引入一个链接?您不能简单地获取所需的
角色
,然后执行类似于Wait\u context.UserRoles.CountAsync的操作吗(ur=>ur.RoleId==theroleimlooking.Id);
public class AccountController : Controller
{
private UserManager<IdentityUser> userManager;
private RoleManager<IdentityRole> roleManager;
public AccountController(UserManager<IdentityUser> userMgr,
RoleManager<IdentityRole> roleMgr)
{
userManager = userMgr;
roleManager = roleMgr;
}
}