【问题标题】:Microsoft Graph - Memberof fields are nullMicrosoft Graph - Memberof 字段为空
【发布时间】:2020-07-30 15:05:10
【问题描述】:

我试图在 C# 中获取用户所属的组。为此,我一直在使用 /memberOf 端点,如下所示:

public async Task<IUserMemberOfCollectionWithReferencesPage> Test()
{
    var graphClient = GetGraphClient(config);

    return await graphClient
        .Users["user@contoso.com"]
        .MemberOf
        .Request()
        .GetAsync();
}

get graph 客户端函数为:

public static GraphServiceClient GetGraphClient(Config config)
{
    var graphClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
    {

        // get an access token for Graph
        var accessToken = GetAccessToken(config).Result;

        requestMessage
            .Headers
            .Authorization = new AuthenticationHeaderValue("bearer", accessToken);

        return Task.FromResult(0);
    }));

    return graphClient;
}

问题是响应只是:

[
{
   "deletedDateTime":null,
   "id":"48215c29-5442-4fbe-b6b4-ce3e1b09157a",
   "oDataType":"#microsoft.graph.group",
   "additionalData":{
      "deletedDateTime":null,
      "classification":null,
      "createdDateTime":null,
      "creationOptions":[

      ],
      "description":null,
      "displayName":null,
      "expirationDateTime":null,
      "isAssignableToRole":null,
      "mail":null,
      "mailEnabled":null,
      "mailNickname":null,
      "membershipRule":null,
      "membershipRuleProcessingState":null,
      "onPremisesDomainName":null,
      "onPremisesLastSyncDateTime":null,
      "onPremisesNetBiosName":null,
      "onPremisesSamAccountName":null,
      "onPremisesSecurityIdentifier":null,
      "onPremisesSyncEnabled":null,
      "preferredDataLocation":null,
      "preferredLanguage":null,
      "renewedDateTime":null,
      "resourceBehaviorOptions":[

      ],
      "resourceProvisioningOptions":[

      ],
      "securityEnabled":null,
      "securityIdentifier":null,
      "theme":null,
      "visibility":null
   }
}
]

该应用具有以下权限:

Directory.AccessAsUser.All
User.Read.All
User.ReadWrite.All
UserAuthenticationMethod.Read.All
UserAuthenticationMethod.ReadWrite.All
Directory.Read.All
Directory.ReadWrite.All
Group.Read.All
GroupMember.Read.All
User.Read

我使用的每个其他端点,增量,用户,订阅,都按预期工作,但由于未知原因,它们在 /memberOf 中只是空值。 我真的希望你们能帮助解决我做错的事情,因为我的头撞墙太久了:)。

提前致谢。

【问题讨论】:

  • 请尝试使用图形浏览器并检查预期的结果集
  • @SruthiJ-MSFTIdentity 感谢您的评论。使用图形资源管理器,我得到了预期的结果,在邮递员中也是如此。我还获得了 azure 广告应用程序的访问令牌,并在邮递员中进行了尝试并获得了预期的结果。虽然某些字段在预期结果中为空,但大多数都已填写。据我所知,C# SDK 似乎是它失败的地方。
  • 请试试这个sample var result = await graphClient.Users[upn].MemberOf .Request() .GetAsync();
  • 感谢@SruthiJ-MSFTIdentity。原来解决方案是来自基本控制器的 GetAllPagesAsType 函数。使用该功能,我得到了结果。原来问题是铸造问题之一。
  • 感谢您的确认,我可以移动 cmets 来回答,以便它可以帮助其他人

标签: c# asp.net-core microsoft-graph-api microsoft-graph-sdks


【解决方案1】:

要获取用户 MemberOf,请在以下示例中使用以下查询

 var result = await graphClient.Users[upn].MemberOf .Request() .GetAsync(); 

使用页面迭代器获取所有目录对象

protected async Task<List<T>> GetAllPagesAsType<T>(
                GraphServiceClient graphClient,
                ICollectionPage<DirectoryObject> page) where T : class
            {
                var allItems = new List<T>();
    
                var pageIterator = PageIterator<DirectoryObject>.CreatePageIterator(
                    graphClient, page,
                    (item) => {
                        // This code executes for each item in the
                        // collection
                        if (item is T)
                        {
                            // Only add if the item is the requested type
                            allItems.Add(item as T);
                        }
    
                        return true;
                    }
                );
    
                await pageIterator.IterateAsync();
    
                return allItems;
            }

请参阅此示例以获取more detail

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多