简体   繁体   English

Microsoft Graph - 字段成员为 null

[英]Microsoft Graph - Memberof fields are null

I've trying to get the groups a user is a member of, in C#.我试图在 C# 中获取用户所属的组。 To do so i've been using the /memberOf endpoint, like so:为此,我一直在使用 /memberOf 端点,如下所示:

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

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

The get graph client function is:获取图客户端 function 为:

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;
}

The problem is that the response is just:问题是响应只是:

[
{
   "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
   }
}
]

The app has the following permissions:该应用程序具有以下权限:

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

Every other endpoint im using, the delta, user, subscriptions, work as intended but for unknow reasons they are just null with /memberOf.我正在使用的所有其他端点、增量、用户、订阅都按预期工作,但由于未知原因,它们只是带有 /memberOf 的 null。 I really hope you guys can help, with what im doing wrong, because i've been banging my head against a wall for way too long:).我真的希望你们能帮助解决我做错的事情,因为我的头撞墙太久了:)。

Thanks in advance.提前致谢。

To Get the user MemberOf please use below query in the below sample要获取用户 MemberOf,请在以下示例中使用以下查询

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

Uses a page iterator to get all directoryObjects使用页面迭代器获取所有目录对象

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;
            }

Please refer to this sample for more detail有关详细信息,请参阅此示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM