簡體   English   中英

如何獲取用戶的組名是使用 Microsoft Graph API 的成員?

[英]How to get group names of the user is a member of using Microsoft Graph API?

我想知道用戶在 AAD 中所屬的組列表。 根據組的成員身份,他們可以做另一個 function。為此,我使用了 MS Graph API,但我只獲得組 ID,而不是組名稱。 我使用圖 API 使用了以下兩種方法。第一種方法使用,

await  graphClient.Users[upn].GetMemberGroups(false).Request().PostAsync()

回復:

[ "fee2c45b-915a-4a64-b130-f4eb9e75525e", "4fe90ae7-065a-478b-9400-e0a0e1cbd540", "e0c3beaf-eeb4-43d8-abc5-94f037a65697" ]

使用的第二種方法,

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

回復:

[
    {
        "deletedDateTime": null,
        "id": "1c4934554-5006-403a-bf4f-bsdfeerwfs6fe6f",
        "oDataType": "#microsoft.graph.group",
        "additionalData": {
            "creationOptions": [],
            "isAssignableToRole": null,
            "resourceBehaviorOptions": [],
            "resourceProvisioningOptions": []
        }
    },
    {
        "deletedDateTime": null,
        "id": "f8975c2f-7651d-4f3a-1234-4ec3808a0ac2",
        "oDataType": "#microsoft.graph.group",
        "additionalData": {
            "creationOptions": [
                []
            ],
            "isAssignableToRole": null,
            "resourceBehaviorOptions": [],
            "resourceProvisioningOptions": [
                []
            ]
        }
    }
]

我需要的是組名和組 ID。 我如何以有效的方式將兩者結合在一起? 或者還有其他方法可以嘗試嗎?

對我來說,以下是獲取組名的方法:

var page = await graphClient
    .Users[userObjectId]
    .MemberOf
    .Request()
    .GetAsync();

var names = new List<string>();
names.AddRange(page
        .OfType<Group>()
        .Select(x => x.DisplayName)
        .Where(name => !string.IsNullOrEmpty(name)));
while (page.NextPageRequest != null)
{
    page = await page.NextPageRequest.GetAsync();
    names.AddRange(page
        .OfType<Group>()
        .Select(x => x.DisplayName)
        .Where(name => !string.IsNullOrEmpty(name)));
}

return names;

組的響應中應該有一個 DisplayName/displayName。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM