【问题标题】:Get members of a Security Group from Azure AD via Microsoft Graph通过 Microsoft Graph 从 Azure AD 获取安全组的成员
【发布时间】:2021-11-19 05:01:32
【问题描述】:

我一直在尝试使用来自 Graph api 的以下代码从 Azure AD 获取特定安全组的成员

var members = await graphClient.Groups["{group-id}"].Members
    .Request()
    .GetAsync();   

我点击了以下链接,该链接表示为注册的应用程序链接授予以下权限:https://docs.microsoft.com/en-us/graph/api/group-list-members?view=graph-rest-1.0&tabs=csharp 并且该应用程序的权限已被管理员授予;

但我不断收到以下错误

ServiceException:代码:Authorization_RequestDenied 消息:权限不足,无法完成操作

【问题讨论】:

  • 请编辑您的问题并包含用于创建图形客户端的代码。您是为用户还是应用程序获取令牌?

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


【解决方案1】:

第一步:您必须注册一个 AD 应用程序并授予图表读取用户和组的权限,请查看thisstackoverflow 答案

【讨论】:

  • 通常不鼓励仅链接的答案。如果您认为该问题之前已在此处回答,您可以随时将问题标记为重复。谢谢。
【解决方案2】:

Using a client secret 创建graphClient。我授予如下许可,它对我有用。您也可以使用其他提供商来执行此操作。

我的测试代码

    public async Task<JsonResult> test()
    {
        // Values from app registration
        var clientId = "fb2****-29ee-****-ab90-********0c7e1";
        var clientSecret = "w7N*******........***yO8ig";

        var scopes = new[] { "https://graph.microsoft.com/.default" };

        // Multi-tenant apps can use "common",
        // single-tenant apps must use the tenant ID from the Azure portal
        var tenantId = "e4c9ab4e-****-40d5-****-230****57fb";

        var options = new TokenCredentialOptions
        {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
        };

        // https://docs.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
        var clientSecretCredential = new ClientSecretCredential(
            tenantId, clientId, clientSecret, options);

        var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

        try
        {
            var members = await graphClient.Groups["13ad4665-****-43e9-9b0f-ca****eb"].Members.Request().GetAsync();
            return Json(members);
        }
        catch (Exception e)
        {
            return Json("");
            throw;
        }
    }

我的测试结果

【讨论】:

    猜你喜欢
    • 2020-06-11
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多