是否有 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();
}