我正在尝试从 Azure 函数内的 ms 图中读取用户属性。 对于身份验证,我使用了 Azure.Identity 中的 DefaultAzureCredential 类。
使用本地共享令牌缓存凭据和 Azure 中的托管身份凭据进行访问是没有问题的! 我想使用 Visual Studio Code 凭据,但在调用图形 API 时收到“Authorization_RequestDenied!权限不足,无法完成操作”错误消息。
问题似乎是我通过 VS Code 凭据收到的访问令牌。用户帐户与我用于共享令牌缓存凭据的帐户相同。
有什么想法吗?谢谢。
代码:
DefaultAzureCredentialOptions options = new DefaultAzureCredentialOptions();
options.VisualStudioCodeTenantId = Environment.GetEnvironmentVariable("Debug_VisualStudioCodeTenantId");
var credential = new DefaultAzureCredential(options);
token = credential.GetToken(
new Azure.Core.TokenRequestContext(
new[] { "https://graph.microsoft.com/.default" }));
accessToken = token.Token;
var graphServiceClient = new GraphServiceClient(
new DelegateAuthenticationProvider((requestMessage) =>
{
requestMessage
.Headers
.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
return Task.CompletedTask;
}));
var users = await graphServiceClient.Users.Request().GetAsync(); // throw the forbidden exception
<块引用>
例外: "代码:Authorization_RequestDenied\r\n消息:权限不足,无法完成操作。\r\n内部错误:\r\n\tAdditionalData:\r\n\tdate: 2021-04-20T08:02:23\r\n\ trequest-id: ...\r\n\tclient-request-id: ...\r\nClientRequestId: ...\r\n"
答案 0 :(得分:0)
检查 VS Code 返回的令牌后,似乎缺少所需的委托权限/范围。 文档说列出用户需要其中之一:
<块引用>User.ReadBasic.All、User.Read.All、User.ReadWrite.All、Directory.Read.All、Directory.ReadWrite.All、Directory.AccessAsUser.All
由于 VS Code 使用的服务主体不需要任何这些,所以它不会工作。 在尝试明确获取具有所需范围的令牌后,它似乎也不起作用。
因此 VS Code 凭据目前似乎不适用于此目的。 您需要使用不同的凭据,或者在自己的应用注册中使用客户端机密/证书凭据。