未找到范围 (scp) 中的 Azure AD 访问令牌

Nim*_*ami 2 c# azure-active-directory microsoft-graph-api

我在 Azure AD 中创建了一个多租户应用程序当我尝试获取访问令牌并签入 jwt.io 时,我发现 scp(范围)丢失。

//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";
Run Code Online (Sandbox Code Playgroud)

我尝试了很多权威 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;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

Tom*_*SFT 5

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

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

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

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


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

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

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

在此输入图像描述