请求所有用户时,如何从 Microsoft Graph API 获取更多默认属性?

How do I get more than the default attributes from the Microsoft Graph API when requesting all users?

我正在尝试从一个具有指定架构扩展的 AAD 租户获取所有用户。 但是,在执行此请求时:

GraphServiceClient 客户端 = new GraphServiceClient(new AuthProv(_authHelper.GetAuthenticationResult().Result));

  var userList = new List<User>();
  var users = await client.Users.Request().GetAsync();


  userList.AddRange(users.CurrentPage);
  while (users.NextPageRequest != null)
  {
    var nextPage = users.NextPageRequest.RequestUrl;
    Debug.WriteLine("Call To: " + users.NextPageRequest.RequestUrl);
    users = users.NextPageRequest.GetAsync().Result;
    userList.AddRange(users);
  }

我收到一个 JSON 对象,看起来像:

[{"businessPhones":[],"displayName":"some account name","userPrincipalName":"somemail@email.com","id":"123", "givenName":null,"jobTitle":null,"mail":null,"mobilePhone":null,"officeLocation":null,"preferredLanguage":null,"surname":null}, ...]

但是,我为用户自定义了一个自己的属性,因此我可以从中检索值,但该属性不会随 API 响应一起发送。

如何更改请求以便将所有用户属性检索为响应?

使用这个新的 baseUrl:“https://graph.microsoft.com/beta/

GraphServiceClient client = new GraphServiceClient("https://graph.microsoft.com/beta/",new AuthProv(_authHelper.GetAuthenticationResult().Result),null);

您似乎使用了开放式扩展程序。如果是,我们需要显式扩展 extensions

下面是打印打开扩展值的代码,供大家参考:

foreach (var user in users.CurrentPage)
{
    if (user.Extensions != null&& user.Extensions.CurrentPage!=null)
    {
        var customProperty = user.Extensions.CurrentPage.FirstOrDefault(ext => ext.Id == "Com.Contoso.Deal");
        if (customProperty != null)
            Console.WriteLine($"{user.UserPrincipalName}--{customProperty.Id}:{customProperty.AdditionalData["companyName"]}");
    }
}

while (users.NextPageRequest != null)
{
    var nextPage = users.NextPageRequest.RequestUrl;
    users = users.NextPageRequest.GetAsync().Result;
    foreach (var user in users.CurrentPage)
    {
        var customProperty = user.Extensions.CurrentPage.First(ext => ext.Id == "Com.Contoso.Deal");
        if (customProperty != null)
            Console.WriteLine($"{user.UserPrincipalName}--{customProperty.Id}:{customProperty.AdditionalData["companyName"]}");

    }
}

如果有多个打开的扩展页面,你也应该通过NextPageRequest来获取它。如果您仍有问题,请随时告诉我。