通过 Web API 控制器读取 Azure AD 组失败 'Insufficient privileges to complete the operation'

Read Azure AD groups by Web API controller fails with 'Insufficient privileges to complete the operation'

我在我公司的 AAD 中注册了一个 Web API 应用程序。这是一个多租户应用程序,能够查询订阅客户的 AAD。当客户订阅我们的服务时,他们会在他们的 AAD 的企业应用程序注册部分注册我们的应用程序,并向我们提供 tenantId、clientId 和 clientSecret。然后我们使用这些参数来查询客户的AAD。

我正在使用以下内容从客户的 Azure AD 中读取组:

    public async Task<GraphServiceClient> CreateGraphServiceClient(string tenantId, string clientId, string clientSecret)
    {
        string aadInstance = "https://login.microsoftonline.com/{0}";
        string resource = "https://graph.microsoft.com";
        string authority = string.Format(aadInstance, tenantId);

        AuthenticationContext authContext = new AuthenticationContext(authority);
        ClientCredential credentials = new ClientCredential(clientId, clientSecret);
        var authResult = await authContext.AcquireTokenAsync(resource, credentials);
        if(authResult == null)
        {
            throw new Exception("GetGraphServiceClient: AcquireTokenAsync failed!");
        }

        var accessToken = authResult.AccessToken;
        var graphServiceClient = new GraphServiceClient(
            new DelegateAuthenticationProvider(
                requestMessageClientOtherTenant =>
                {
                    requestMessageClientOtherTenant.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
                    return Task.FromResult(0);
                }));

        return graphServiceClient;
    }

//....
        var graphServiceClient = CreateGraphClient(tenantId, clientId, clientSecret);

        var graphGroupsPage = await graphServiceClient.Groups
            .Request()
            .Select(e => new {
                e.Id,
                e.DisplayName
            })
            .GetAsync();

我收到 'Insufficient privileges to complete the operation' 错误。

我在网上看到了很多 2-3 年前的解决方法,但没有一个对我有用。

在客户的 "Enterprise applications" 部分中授予应用程序的权限:

这些权限与我们在 AAD 注册中分配给应用程序的权限完全相同。

我在这里不知所措。似乎已授予所有必需的权限,但我仍然遇到错误。

我可以使用同一个图形客户端毫无问题地从客户的 AAD 中获取所有用户。

如有任何建议,我们将不胜感激。

When customers subscribe to our service they register our app in their AAD's Enterprise Applications registration section and provide us with tenantId, clientId, and clientSecret. We then use these parameters to query customer's AAD.

由于您的客户已授权您的多租户应用访问他们的租户信息,因此只需要客户的 tennantId,您应该使用您自己的多租户应用的 clientIdclientSecret 访问客户的广告。

如果您使用客户的 client_id 和秘密,您应该检查他们的应用程序(不是企业应用程序)下的权限,但是,这没有任何意义。如果是这样,则无需使用您的多租户应用程序。