c# - 访问 token 不包含通过 Microsoft Graph 的 SCP(角色)声明

标签 c# azure azure-active-directory microsoft-graph-api microsoft-graph-sdks

我正在使用 Microsoft Graph SDK 获取我的应用程序(而不是用户)的访问 token ,以便从共享点读取数据。我一直在关注这个document ,以及发布此SO question 。链接 SO 中的代码是相同的。我能够添加应用程序权限并在 azure 门户中授予它们(通过按按钮)。问题是,返回使用的 token 不包含任何角色/scp 声明。因此,在使用 token 时,我收到“ token 中需要存在 scp 或角色声明”消息。

为了确定,我在获取访问 token 时传递的范围的唯一值是:https://graph.microsoft.com/.default。我没有传递像 Sites.ReadWrite.All 这样的其他内容(如果我添加该范围,我会得到一个异常)。我不确定如何继续进行故障排除,我们将不胜感激。

编辑:使用如下所示的图形 SDK 添加代码:

var client = new ConfidentialClientApplication(id, uri, cred, null, new SessionTokenCache());
var authResult = await client.AcquireTokenForClientAsync(new[] {"https://graph.microsoft.com/.default"});
var token = authResult.AccessToken;
var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider(async request => {request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token)}));
var drives = await graphServiceClient.Sites[<sharepoint_host>].SiteWithPath(<known_path>).Drives.Request().GetAsync(); 

最佳答案

似乎以不同的方式进行应用程序初始化是解决方案。而不是这个:

var client = new ConfidentialClientApplication(id, uri, cred, null, new SessionTokenCache());

这样做:

var app = new ConfidentialClientApplication(ClientId, Authority, RedirectUri, credentials, null, new TokenCache());

关于c# - 访问 token 不包含通过 Microsoft Graph 的 SCP(角色)声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55830228/

相关文章:

c# - hangfire 配置中的 SchedulePollingInterval、HeartbeatInterval 和 ServerCheckInterval 有什么区别?

c# - 我需要使用MS Debug Diagnostic Tool来分析CPU使用率较高的C#应用​​程序的帮助

从 API 调用 Microsoft Graph 时 Azure AD B2C 重置密码选项

c# - WPF c# Checkbox可点击区域修改-BATCH

C#如何使用C#向mysql中插入数据?

python - 首选 Azure 服务以可扩展的方式使用端点运行 python 脚本

azure - 找不到类型 Microsoft.WindowsAzure.Commands.SqlDatabase.Services.ImportExportRequest

azure - 如何在 Microsoft Azure 中检查/查看 SQL DB 中的数据

azure-active-directory - 未返回预期更改的组上的图形增量查询

azure-active-directory - Azure 广告 B2C Multi-Tenancy