Azure GraphClient 按组名获取用户

Azure GraphClient Get Users By Group Names

我正在尝试通过组名列表从 AAD 中检索用户列表。

此代码:

var roleGroups = new string[] { "Group Name 1", "Group Name 2" };

ActiveDirectoryClient client = AuthenticationHelper.GetActiveDirectoryClient();

var users = client.Users.Where(u => u.CheckMemberGroupsAsync(roleGroups).Result.Any());

投掷:

CheckMemberGroupsAsync declared on type 'Microsoft.Azure.ActiveDirectory.GraphClient.IDirectoryObject' cannot be called with instance of type 'Microsoft.Azure.ActiveDirectory.GraphClient.Internal.User'

我也试过:

  client.Users.Where(u =>((User)u).MemberOf.OfType<Group>()             
   .Any(g => roleGroups.Contains(g.DisplayName))).ExecuteAsync()         
    .Result.CurrentPage.Select(u => (User)u).ToList();

client.Users.Where(u => u.GetMemberGroupsAsync(null).Result.Any(g => roleGroups.Contains(g)));

抛出:

GetMemberGroupsAsync declared on type 'Microsoft.Azure.ActiveDirectory.GraphClient.IDirectoryObject' cannot be called with instance of type 'Microsoft.Azure.ActiveDirectory.GraphClient.Internal.User'

如何通过组名列表获取用户列表 (Microsoft.Azure.ActiveDirectory.GraphClient.User)?

我找到了一种方法。

 var groupUsers = new List<User>();

            foreach (var groupName in roleGroups)
            {
                var groups = client.Groups.Where(g => g.DisplayName == groupName).Expand(g => g.Members)
                    .ExecuteAsync()
                    .Result.CurrentPage.ToList();

                var users = groups.SelectMany(g => g.Members.CurrentPage.Select(m => m as User)).Where(u => u != null);

                if (users.Any())
                {
                    groupUsers.AddRange(users);
                }
            }

但我仍然相信有更好的解决方案。

请注意 Expand(g => g.Members) 将 return 最多 20 个对象。而且,如果你的 Azure AD 中有很多用户,你必须遍历所有页面,而不仅仅是 g.Members.CurrentPage