首页 > 解决方案 > 在 Graph API 中列出组成员的权限不足

问题描述

我正在构建一个由 SPA(角度)调用的 API。用户使用 Azure AD 进行身份验证,API 使用 AzureAdBearer 进行身份验证。

我需要后端代表用户调用图形 api 以列出 AD 组的成员。

根据此文档,我需要以下权限之一:

User.ReadBasic.All, User.Read.All, Group.Read.All, Directory.Read.All

因此,我在后端的应用程序注册中将 User.ReadBasic.All 添加到了我的 api 权限列表中。 后端权限

IAuthenticationProvider使用以下代码处理身份验证:

var app = ConfidentialClientApplicationBuilder.Create("{my client id}")
                 .WithAuthority("https://login.microsoftonline.com/{my tenant id}")
                 .WithClientSecret({my backend secret})
                 .WithLogging(Log, Microsoft.Identity.Client.LogLevel.Info, true)
                 .Build();

var token = await _httpContextAccessor.HttpContext.GetTokenAsync("access_token");

var userAssertion = new UserAssertion(token);
var result = await app.AcquireTokenOnBehalfOf(new[] { "https://graph.microsoft.com/.default" }, userAssertion)
                      .ExecuteAsync();

request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);

结果是成功的,当我使用 jwt.ms 检查我的令牌时,一切似乎都是正确的,这是它的摘录:

"aud": "https://graph.microsoft.com",
"iss": "https://sts.windows.net/{my tenant id}/",
[...]
"app_displayname": "My app",
"appid": "{my client id}",
[...]
"scp": "User.ReadBasic.All profile openid email",
[...]

但是在使用 GraphApiClient 的这个调用中:

var client = new GraphServiceClient(authProvider);
var users = await client.Groups["group guid"].Members.Request().GetAsync();

我收到此错误:

Code: Authorization_RequestDenied
Message: Insufficient privileges to complete the operation.

标签: .net-coremicrosoft-graph-api

解决方案


此调用var users = await client.Groups["group guid"].Members.Request().GetAsync();需要组权限(即Group.Read.All)。

您现在只有用户权限(即User.ReadBasic.All)。


推荐阅读