我正在构建一个由SPA(角度)调用的API。使用Azure AD对用户进行身份验证,并且API使用AzureAdBearer进行身份验证。
我需要后端代表用户调用图api才能列出广告组的成员。
According to this documentation,我需要以下权限之一:
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.
答案 0 :(得分:3)
此呼叫var users = await client.Groups["group guid"].Members.Request().GetAsync();
需要一个group permission(即Group.Read.All
)。
您现在只有user permissions(即User.ReadBasic.All
)。