0

是否有 Microsoft Graph API 来找出 AAD 组中的用户数?目前,这是我如何找到它的代码。想知道是否有更快的方法?

            private async Task<int> GetUserIds(string groupId)
            {
                List<string> userIds = new List<string>();

                var usersFromGroup = await _groupMembersService.GetGroupMembersPageByIdAsync(groupId);
                usersFromGroup.AdditionalData.TryGetValue("@odata.nextLink", out object nextLink);
                var nextPageUrl = (nextLink == null) ? string.Empty : nextLink.ToString();
                userIds.AddRange(usersFromGroup.OfType<Microsoft.Graph.User>().Select(x => x.Id));

                while (!string.IsNullOrEmpty(nextPageUrl))
                {
                    usersFromGroup = await _groupMembersService.GetGroupMembersNextPageAsnyc(usersFromGroup, nextPageUrl);
                    usersFromGroup.AdditionalData.TryGetValue("@odata.nextLink", out object nextLink2);
                    nextPageUrl = (nextLink2 == null) ? string.Empty : nextLink2.ToString();
                    userIds.AddRange(usersFromGroup.OfType<Microsoft.Graph.User>().Select(x => x.Id));
                }

                return userIds.Count;
            }
        }

        public async Task<IGroupTransitiveMembersCollectionWithReferencesPage>GetGroupMembersPageByIdAsync(string groupId)
        {
            return await this.graphServiceClient
                                    .Groups[groupId]
                                    .TransitiveMembers
                                    .Request()
                                    .Top(this.MaxResultCount)
                                    .WithMaxRetry(this.MaxRetry)
                                    .GetAsync();
        }

        public async Task<IGroupTransitiveMembersCollectionWithReferencesPage> GetGroupMembersNextPageAsnyc(
            IGroupTransitiveMembersCollectionWithReferencesPage groupMembersRef,
            string nextPageUrl)
        {
            groupMembersRef.InitializeNextPageRequest(this.graphServiceClient, nextPageUrl);
            return await groupMembersRef
                .NextPageRequest
                .GetAsync();
        }
4

1 回答 1

0

您可以使用此图形 API 来获取任何组的计数。 https://graph.microsoft.com/v1.0/groups/{group-object-id}/members/$count

确保为此添加ConsistencyLevel = Eventual请求标头。在 Graph Explorer 中为您测试:

MS 图形资源管理器屏幕截图

于 2021-08-06T04:52:42.190 回答