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)
)
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();
8条答案
按热度按时间vfh0ocws1#
在1.0 RTM中不可能通过RoleManager,在1.1中它将通过IQueryable RoleManager.Roles公开。对于1.0,您需要下拉到实现层(即数据库上下文)
fkvaft9z2#
你可以使用Entity Framework,但是使用ASP.NET Identity 1.0还不可能。你必须等待Identity 2.0的发布。
字符串
k10s72fa3#
这是新的
MVC 5 ASP.NET Identity
:字符串
guykilcj4#
最简单的方法让用户在任何角色
字符串
ogsagwnx5#
这应该适用于.net core 5和更高版本。
第一个月
uubf1zoe6#
我没有看到一个内置的方式,但它是相当容易实现。我有这个方法在我的应用程序特定的UserManager:
字符串
它输出的SQL似乎是合理的:
型
gopyfrb37#
出于某种原因,@ChoptimusPrime上面建议的非常好的查询在ASP.NET Identity 2.2.1中没有编译。我写了一个扩展函数:
字符串
xoshrz7s8#
例如,如果您希望用户列表具有角色“User”
字符串