如何获取角色中所有用户的列表?在使用 Roles.GetUsersInRole 之前,但使用新身份时,我找不到这样的东西。
问问题
27263 次
7 回答
12
我没有看到内置的方法,但它很容易实现。我在我的应用程序特定的 UserManager 中有这个方法:
public IQueryable<User> GetUsersInRole(string roleName)
{
return from user in Users
where user.Roles.Any(r => r.Role.Name == roleName)
select user;
}
它输出的 SQL 似乎是合理的:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Email] AS [Email],
[Extent1].[EmailConfirmed] AS [EmailConfirmed],
[Extent1].[PasswordHash] AS [PasswordHash],
[Extent1].[SecurityStamp] AS [SecurityStamp],
[Extent1].[PhoneNumber] AS [PhoneNumber],
[Extent1].[PhoneNumberConfirmed] AS [PhoneNumberConfirmed],
[Extent1].[TwoFactorEnabled] AS [TwoFactorEnabled],
[Extent1].[LockoutEndDateUtc] AS [LockoutEndDateUtc],
[Extent1].[LockoutEnabled] AS [LockoutEnabled],
[Extent1].[AccessFailedCount] AS [AccessFailedCount],
[Extent1].[UserName] AS [UserName]
FROM [dbo].[AspNetUsers] AS [Extent1]
WHERE EXISTS (SELECT
1 AS [C1]
FROM [dbo].[AspNetUserRoles] AS [Extent2]
INNER JOIN [dbo].[AspNetRoles] AS [Extent3] ON [Extent2].[RoleId] = [Extent3].[Id]
WHERE ([Extent1].[Id] = [Extent2].[UserId]) AND ([Extent3].[Name] = @p__linq__0)
)
于 2014-05-29T19:01:33.177 回答
11
出于某种原因,@ChoptimusPrime 上面建议的非常好的查询在 ASP.NET Identity 2.2.1 中没有为我编译。我写了一个扩展函数:
public static IQueryable<User> GetUsersInRole(DbContext db, string roleName)
{
if (db != null && roleName != null)
{
var roles = db.Roles.Where(r => r.Name == roleName);
if (roles.Any())
{
var roleId = roles.First().Id;
return from user in db.Users
where user.Roles.Any(r => r.RoleId == roleId)
select user;
}
}
return null;
}
于 2016-01-26T13:50:40.590 回答
4
它不可能通过 1.0 RTM 中的 RoleManager,在 1.1 中它将通过 IQueryable RoleManager.Roles 公开。对于1.0,需要下拉到实现层(即db context)
于 2013-10-21T21:25:37.587 回答
3
例如,如果您想要角色为“用户”的用户列表
var users = await (from user in _dbContext.Users
join userRole in _dbContext.UserRoles
on user.Id equals userRole.UserId
join role in _dbContext.Roles
on userRole.RoleId equals role.Id
where role.Name == "User"
select user)
.ToListAsync();
于 2021-01-15T10:15:04.527 回答
0
您可以使用实体框架,但还不能使用 Asp.Net Identity 1.0。您必须等待 Identity 2.0 的发布。
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString)) {
string queryString = "SELECT AspNetUsers.UserName FROM dbo.AspNetUsers INNER JOIN dbo.AspNetUserRoles ON " + "AspNetUsers.Id=AspNetUserRoles.UserId WHERE AspNetUserRoles.RoleID='" + id + "'";
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
List<string> @out = null;
dynamic reader = command.ExecuteReader();
while (reader.Read()) {
if (@out == null) @out = new List<string>();
@out.Add(reader.GetString(0));
}
return @out;
}
于 2014-02-25T18:57:55.130 回答
0
这是针对新的MVC 5 ASP.NET Identity
:
var managerRole = TMRoles.GetIdentityRole(TMRoles.Manager);
var managers = managerRole.Users;
public class TMRoles
{
private static RoleManager<IdentityRole> RoleManager =
new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new TMContext()));
public static string Manager { get { return "Manager"; } }
public static IdentityRole GetIdentityRole(string roleName)
{
return RoleManager.FindByName(roleName);
}
}
于 2014-06-17T02:45:50.730 回答
0
让用户担任任何角色的最简单方法
int totalUser=db.AspNetUsers.Where(u => u.AspNetRoles.Any(r => r.Name == "USER")).Count()
于 2020-09-27T17:05:10.227 回答