2013-10-21 49 views
7

如何获取角色中所有用户的列表?在使用Roles.GetUsersInRole之前,有了新身份之前,我找不到像这样的东西。ASP.NET身份:获取角色中的所有用户

+1

这可能是惊人昂贵发现一个角色的成员,包括重复远程查询到任何数据存储实际上是支持您所接受的身份 - 即使这些商店*支持*这样的查询。试着把事情放在首位 - 为什么你需要知道角色的所有成员? (并且记住,只要系统有这些信息,它可能会过时) –

+1

@Damien_The_Unbeliever:我需要将应用程序从旧成员身份迁移到新身份。例如,我需要有一个管理员角色的用户列表才能发送电子邮件给他们所有人。现在我试图想象一个数据库查询,看起来像是一个内部联接的简单查询。也许我错过了很多,但我不明白为什么这是非常昂贵的。所以,我总是可以通过数据库上下文来做到这一点,但只是想知道为什么框架不提供这种功能。 – graycrow

+0

我说它*可能会很贵 - 并非所有使用身份系统的场景都具有存储在SQL数据库中的角色。 –

回答

5

它不可能通过1.0 RTM中的RoleManager在1.1中通过IQueryable RoleManager.Roles公开。对于1.0,您需要下拉到实现层(即数据库上下文)

+5

你可以给一些代码吗? – daniel

+1

'context.Roles' – nandin

+0

你能解释一下吗?给我们一些代码或例子。谢谢 –

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; 
} 
+0

最好是使用Entity Framework与身份然后它是下降到SqlConnections和SqlCommands。 IMO –

10

我没有看到内置的方式,但它很容易实现。我有这个方法在我的应用程序特定的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) 
) 
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); 
    } 
} 
4

出于某种原因,很上面的@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; 
}