如何从Azure AD获取用户属性

时间:2018-01-23 14:25:52

标签: c# azure active-directory

我使用MS library中的标准摘要从我的 azure AD 中获取用户及其属性。但不幸的是,我发现这个代码段并没有获得用户拥有的所有属性,只获取其显示名称,全名,姓氏,其他所有属性都是null,但我需要获取用户的所有属性有。我已经很多谷歌这个问题,只找到了不适合我的 LDAP解决方案。

用户属性的JSON输出图像:

Image of JSON output of user properties

[{"Properties":{},"Id":null,"Display":null,"Department":null,"GivenName":"Example","Surname":"Example","UserPrincipalName":"Example@Example.com"},{"Properties":{},"Id":null,"Display":null,"Department":null,"GivenName":"Example","Surname":"Example","UserPrincipalName":"Example@Example.com"},{"Properties":{},"Id":null,"Display":null,"Department":null,"GivenName":"Example","Surname":"Example","UserPrincipalName":"Example@Example.com"},{"Properties":{},"Id":null,"Display":null,"Department":null,"GivenName":"Example","Surname":"Example","UserPrincipalName":"Example@Example.com"},{"Properties":{},"Id":null,"Display":null,"Department":null,"GivenName":"Example","Surname":"Example","UserPrincipalName":"Example@Example.com"},{"Properties":{},"Id":null,"Display":null,"Department":null,"GivenName":"Example","Surname":"Example","UserPrincipalName":"Example@Example.com"},

所以问题是,如何使用C#从Azure AD获取所有用户属性?

我的用户模型:

 public async Task<List<ResultsItem>> GetUsers(GraphServiceClient graphClient)
    {
        List<ResultsItem> items = new List<ResultsItem>();

        // Get users.
        IGraphServiceUsersCollectionPage users = await graphClient.Users.Request().GetAsync();

        // Populate the view model.
        if (users?.Count > 0)
        {
            foreach (User user in users)
            {

                // Filter out conference rooms.
                string displayName = user.DisplayName ?? "";
                if (!displayName.StartsWith("Conf Room"))
                {

                    // Get user properties.
                    items.Add(new ResultsItem
                    {
                        Mail = user.Mail,
                        Display = user.DisplayName,
                        Id = user.Id,
                        Department = user.Department,
                        GivenName = user.GivenName,
                        Surname = user.Surname,
                        UserPrincipalName = user.UserPrincipalName                           
                    });
                }
            }
        }
      var jsonEmployees = JsonConvert.SerializeObject(items); // Convert to JSON
      return items;
    }

1 个答案:

答案 0 :(得分:2)

根据您的说明,我注册了AD v2.0应用,并使用MSAL通过 User.Read User.ReadBasic.All 检索访问令牌范围。在获取用户时使用fiddler捕获网络跟踪,我可以按如下方式获得响应:

enter image description here

Microsoft Graph List users声明如下:

  

默认情况下,仅返回一组有限的属性(businessPhones,displayName,givenName,id,jobTitle,mail,mobilePhone,officeLocation,preferredLanguage,surname,userPrincipalName)。

     

要返回备用属性集,必须使用OData $select查询参数指定所需的用户属性集。例如,要返回displayName,givenName和postalCode,您可以使用将以下内容添加到查询$select=displayName,givenName,postalCode

简单来说,我只需使用Microsoft Graph Explorer并访问https://graph.microsoft.com/v1.0/users?$select=Department,Mail,Display,Id,GivenName,Surname,UserPrincipleName,如下所示:

enter image description here

使用客户端库时,可以使用以下代码:

var users = await graphClient.Users.Request().Select("Department,Mail,Display,Id,GivenName,Surname,UserPrincipleName").GetAsync();

详细信息,您可以按照Microsoft Graph .NET Client Library Overview下的查询选项部分。

此外,您需要检查是否有更多记录并通过以下代码段检索所有用户:

var userList = new List<User>();
IGraphServiceUsersCollectionPage pagedCollection = await graphClient.Users.Request().Select("Department,Mail,Display,Id,GivenName,Surname,UserPrincipleName").GetAsync();
if (pagedCollection != null)
{
    do
    {
        List<User> usersList = pagedCollection.CurrentPage.ToList();
        foreach (var user in usersList)
        {
            userList.Add(user);
        }
        pagedCollection = await pagedCollection.NextPageRequest.GetAsync();
    } while (pagedCollection != null);
}

<强>更新

我利用Microsoft Graph Snippets Sample for ASP.NET 4.6示例并注册了我的AD V2.0应用,并替换了我的web.config文件下的ida:AppIdida:AppSecret设置。它可以在我这边工作如下:

enter image description here

使用fiddler,我可以捕获https://graph.microsoft.com/v1.0/users的网络跟踪,并且可以正确返回用户属性。我建议您调用应用程序并在调用await graphClient.Users.Request().GetAsync();后检查结果,并在迭代user集合时检查users实例的属性。