仅从Azure AD-Graph API接收100个用户



我们从下面的代码(Asp.Net Core Api(中只收到100个用户。我们希望接收来自Azure AD的所有用户

var token = await _tokenAcquisition.GetAccessTokenForUserAsync("User.Read.All");
var client = _clientFactory.CreateClient();
client.BaseAddress = new Uri("https://graph.microsoft.com/v1.0");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var graphClient = new GraphServiceClient(client)
{
AuthenticationProvider = new DelegateAuthenticationProvider((requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
return Task.CompletedTask;
}),
BaseUrl = "https://graph.microsoft.com/v1.0"
};
return graphClient.Users.Request().GetAsync();

MS Graph使用分页输出,默认页面大小为100条记录。您只阅读了默认100个用户的第一页,就可以使用顶部(999(

请检查样本代码

var users = await graphServiceClient
.Users
.Request()
.Top(999)  // <- Custom page of 999 records (if you want to set it)
.GetAsync()
.ConfigureAwait(false);
while (true) {
//TODO: relevant code here (process users)
// If the page is the last one
if (users.NextPageRequest is null)
break;
// Read the next page
users = await users
.NextPageRequest
.GetAsync()
.ConfigureAwait(false);
} 

最新更新