【问题标题】:How to get group names of the user is a member of using Microsoft Graph API?如何获取用户的组名是使用 Microsoft Graph API 的成员?
【发布时间】:2020-12-09 15:53:03
【问题描述】:

我想知道用户在 AAD 中所属的组列表。根据组的成员身份,他们可以执行其他功能。为了实现这一点,我使用了 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。我如何以有效的方式将两者结合在一起?或者有什么其他方法可以尝试吗?

【问题讨论】:

    标签: c# azure azure-active-directory microsoft-graph-api asp.net-core-webapi


    【解决方案1】:

    对我来说,获取组名的方法如下:

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

    在组的响应中应该有一个 DisplayName/displayName。

    【讨论】:

    • 代码应该返回名称,而不是 groupNames。尽管 Id 对于预期的组是正确的,但我的 DisplayName 值为 null。
    • 更正:我必须将 Directory.Read.All 权限添加到我的 B2C 应用注册中。现在我得到了返回的 DisplayName。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 2020-06-10
    相关资源
    最近更新 更多