c# - 未找到范围 (scp) 中的 Azure AD 访问令牌
问题描述
我在 Azure AD 中创建了一个多租户应用程序当我尝试获取访问令牌并签入 jwt.io 时,我发现 scp (Scope) 丢失。
//string authority = "https://login.microsoftonline.com/{0}/common/oauth2/v2.0/token?&response_type=code&scope=openid%20profile%20User.ReadWrite%20User.ReadBasic.All%20Sites.ReadWrite.All%20Contacts.ReadWrite%20People.Read%20Notes.ReadWrite.All%20Tasks.ReadWrite%20Mail.ReadWrite%20Files.ReadWrite.All%20Calendars.ReadWrite";
//string authority = "https://login.microsoftonline.com/{0}/common/oauth2/v2.0/token?&scope=https://graph.windows.net/directory.read%20https://graph.windows.net/directory.write";
//string authority = "https://login.microsoftonline.com/{0}/common/oauth2/v2.0/token";
//string authority = "https://login.microsoftonline.com/{0}";
//string authority = "https://login.microsoftonline.com/{0}/common/oauth2/v2.0/token?&response_type=code&scope=openid%20profile%20User.Read%20User.ReadWrite%20User.ReadBasic.All";
//string authority = "https://login.microsoftonline.com/{0}/oauth2/token?scope=User.ReadBasic.All";
//string authority = "https://login.microsoftonline.com/{0}/oauth2/token?scope=User.ReadBasic.All";
string authority = "https://login.microsoftonline.com/common/oauth2/v2.0/token?response_type=token&scope=User.ReadBasic.All";
我已经尝试了许多权威 URL 的组合
string graphResourceId = "https://graph.microsoft.com";
string clientId = "XXXX";
string secret = "XXXX";
authority = String.Format(authority, tenantId);
AuthenticationContext authContext = new AuthenticationContext(authority);
var accessToken = authContext.AcquireTokenAsync(graphResourceId, new ClientCredential(clientId, secret)).Result;
如何获取 microsoft.graph 资源的范围?
解决方案
如果它是在运行时作为客户端访问令牌中的“scp”声明呈现给资源的委托权限。
但是您正在使用应用程序权限,该权限使用客户端应用程序的凭据/身份指定基于角色的访问,在运行时作为客户端访问令牌中的“角色”声明呈现给资源。
“委托”权限,使用来自已登录资源所有者的委托授权指定基于范围的访问,在运行时作为客户端访问令牌中的“ scp ”声明呈现给资源。
使用客户端应用程序的凭据/身份指定基于角色的访问的应用程序权限在运行时作为客户端访问令牌中的“角色”声明呈现给资源。
如何获取 microsoft.graph 资源的范围?
我们可以从这个链接中得到答案。
通过选择所需的“委派权限”和“应用程序权限”(后者需要全局管理员角色的成员身份),在 Azure 门户的“应用程序”/“设置”选项卡上的“所需权限”下配置权限请求。由于公共客户端无法安全地维护凭据,因此它只能请求委托权限,而机密客户端可以同时请求委托权限和应用程序权限。客户端的应用程序对象将声明的权限存储在其 requiredResourceAccess 属性中。
推荐阅读
- mysql - 如何强制 MySQL 在磁盘上而不是在内存中写入临时表以进行特定查询?
- javascript - 如何从数组中删除对象并更新所有其他对象的位置值?
- hadoop - Parquet 格式:获取所有列时有用吗?
- alfresco - Uploader plus 不适用于 Alfresco 中的特定站点
- java - 如何正确舍入平方根函数?
- sql - .csv 文件使用 sql bulkload 或 openrowset 导入缺失列数据
- swift - Cocoa:在 Swift 4 的 NSTableView 中添加文本单元格(NSTextField)后得到通知并开始编辑文本单元格?
- jquery - 如何在 jquery datepicker 中禁用整周
- mongodb - 将新项目插入到集合的所有文档到嵌套数组中
- google-cloud-platform - 自定义图像上的 Google 启动脚本不起作用