Microsoft Graph API 分页无法从 Azure AD 获取所有用户 [英] Microsoft Graph API Pagination is not working for getting all users from Azure AD

查看:31
本文介绍了Microsoft Graph API 分页无法从 Azure AD 获取所有用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 Microsoft Graph API Beta 版使用以下代码从 Azure AD 检索所有用户.API 在响应中仅返回 100 个用户,为了使用分页响应,我们尝试了 NextPageRequest 属性.但它总是为 NextPageRequest 属性返回 null.因此,它永远不会进入 while 循环来检索其余用户.

We are using Microsoft Graph API Beta version to retrieve all users from the Azure AD using below code. API returns only 100 users in the response and to use paginated response we tried NextPageRequest property. But it always return null for the NextPageRequest property. And due to that it never enters to the while loop to retrieve rest of the users.

Beta SDK 版本:4.0.1.0

Beta SDK version: 4.0.1.0

代码:

                List<User> usersList = new List<User>();
                IGraphServiceUsersCollectionPage users = await graphClient.Users.Request().GetAsync();

                // Add the first page of results to the user list
                usersList.AddRange(users.CurrentPage);

                // Fetch each page and add those results to the list
                while (users.NextPageRequest != null)
                {
                    users = await users.NextPageRequest.GetAsync();
                    usersList.AddRange(users.CurrentPage);
                }

                log.Info("Users count: " + usersList.Count.ToString());
                return usersList;

我关注的参考链接:

对此的任何帮助将不胜感激!

Any help on this will be appreciated!

推荐答案

下面的代码对我来说非常好.

The below code works perfectly fine for me.

public static async Task<List<User>> getUsers()
        {
            List<User> usersList = new List<User>();
            graphClient.BaseUrl = "https://graph.microsoft.com/beta";
            IGraphServiceUsersCollectionPage users = await graphClient.Users
                .Request()
                .GetAsync();

            usersList.AddRange(users.CurrentPage);

            while (users.NextPageRequest != null)
            {
                users = await users.NextPageRequest.GetAsync();
                usersList.AddRange(users.CurrentPage);
            }
            return usersList;
        }

在 Azure Active Directory 用户刀片中检查您的用户,并查看其中有多少用户.您还可以通过简单地使用 $top 查询参数扩展代码来测试用户数量是否超过 100,每个请求提供 998 条记录,如下所示.

Check your users in Azure Active Directory Users Blade and see how many users are present in it. And also you can test the number of users if there are more than 100 or not by simply extending the code with $top query parameter which gives 998 records per request as shown below.

IGraphServiceUsersCollectionPage users = await graphClient.Users
                    .Request()
                    .Top(998)
                    .GetAsync();

您还可以在 Graph Explorer 中测试 Graph API 调用.

You can also test Graph API calls in Graph Explorer.

经过长时间的研究,我发现它是 Microsoft Graph Beta SDK 中的一个错误,因为它总是在 NextPageRequest 中发送 null.但这里有趣的是,它在 AdditionalData 属性中发送 odata.nextLink.因此,如果您使用的是 Graph Beat SDK,请使用以下代码.

After a long research I got to find out that its a bug in Microsoft Graph Beta SDK as it is always sending null in the NextPageRequest. But the interesting thing here is, it is sending the odata.nextLink in the AdditionalData property. So use the below code if you are using Graph Beat SDK.

public static async Task<List<User>> getUsers()
        {
            List<User> usersList = new List<User>();
            IGraphServiceUsersCollectionPage users = await graphClient.Users
                .Request()
                .GetAsync();

            usersList.AddRange(users.CurrentPage);
            try
            {
                while (users.AdditionalData["@odata.nextLink"].ToString() != null)
                {
                    users.InitializeNextPageRequest(graphClient, users.AdditionalData["@odata.nextLink"].ToString());
                    users = await users.NextPageRequest.GetAsync();
                    usersList.AddRange(users.CurrentPage);
                }
            }
            catch(Exception e)
            {
                
            }
            return usersList;
        }

注意: Microsoft 不建议使用其 Graph Beta 版本,因为它们会发生变化.

Note: Microsoft doesn't suggest to use its Graph Beta version in Production as they are subjected to change.

这篇关于Microsoft Graph API 分页无法从 Azure AD 获取所有用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆