首页 > 解决方案 > 未找到范围 (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 资源的范围?

标签: c#azure-active-directorymicrosoft-graph-api

解决方案


如果它是在运行时作为客户端访问令牌中的“scp”声明呈现给资源的委托权限。

但是您正在使用应用程序权限,该权限使用客户端应用程序的凭据/身份指定基于角色的访问,在运行时作为客户端访问令牌中的“角色”声明呈现给资源。

委托”权限​​,使用来自已登录资源所有者的委托授权指定基于范围的访问,在运行时作为客户端访问令牌中的“ scp ”声明呈现给资源。

使用客户端应用程序的凭据/身份指定基于角色的访问的应用程序权限在运行时作为客户端访问令牌中的“角色”声明呈现给资源。


如何获取 microsoft.graph 资源的范围?

我们可以从这个链接中得到答案。

通过选择所需的“委派权限”和“应用程序权限”(后者需要全局管理员角色的成员身份),在 Azure 门户的“应用程序”/“设置”选项卡上的“所需权限”下配置权限请求。由于公共客户端无法安全地维护凭据,因此它只能请求委托权限,而机密客户端可以同时请求委托权限和应用程序权限。客户端的应用程序对象将声明的权限存储在其 requiredResourceAccess 属性中。

在此处输入图像描述


推荐阅读