【问题标题】:Access token doesn't contain all the requested scopes访问令牌不包含所有请求的范围
【发布时间】:2021-01-22 05:25:43
【问题描述】:

我有一个 angular 应用程序和一个 DotNet 核心 web api 应用程序。 Web api 公开了 2 个权限。角度已订阅这些权限(见屏幕截图)。

在代码中,我试过这个

export const loginRequest: { scopes: string[] } = {
  //scopes: ['user.read', 'openid', 'profile'], //--> I commented out this line. 
  scopes: []
};

export const tokenRequest: { scopes: string[] } = {
  scopes: ['api://da8b9450-d9b7-4b7f-9667-fdae9a7c8359/API.Access',
           'api://da8b9450-d9b7-4b7f-9667-fdae9a7c8359/API.Write']
};

consentScopes: [
  ...loginRequest.scopes,
  ...tokenRequest.scopes,
],

访问令牌包含我没有要求的范围,但它没有返回我要求的范围。

如何获得我请求的 2 个作用域?

感谢您的帮助

编辑 1

这是配置

auth: {
  clientId: '78803184-e866-4966-b372-d98b4feae898',
  authority: "https://login.microsoftonline.com/{tenantId}/",
  validateAuthority: true,
  redirectUri: "http://localhost:4200/",
  postLogoutRedirectUri: "http://localhost:4200/",
  navigateToLoginRequestUrl: true,
}

编辑 2

这是现在请求的范围。我已经删除了所有相关的图表,例如user.readopenidprofile

{
  popUp: !isIE,
  consentScopes: [
    "api://da8b9450-d9b7-4b7f-9667-fdae9a7c8359/API.Access",
    "api://da8b9450-d9b7-4b7f-9667-fdae9a7c8359/API.Write"
  ],
  unprotectedResources: ["https://localhost:5001"],
  protectedResourceMap,
  extraQueryParameters: {}
}

我仍然收到相同的范围,即即使在客户端到 API 的客户端列表之后。

但是,我查看了发送到 AZURE AD 的请求。这就是它的样子。根据此请求,我仍在请求 user.readopenidprofile,尽管我已将它们从请求范围列表中删除。

Request URL: https://login.microsoftonline.com/313200b5-a917-47d1-2233-149b07d5d7b5/oauth2/v2.0/authorize?
response_type=token&scope=user.read openid profile&client_id=78803184-e866-54e3-b200-d98b4feae898
&redirect_uri=http://localhost:4200/
&state=eyJpZCI6IjMwZDJkOGNkLTM2NWUtNGMwOS1iYWY1LTcyZWYyMTU0YWE5ZSIs
InRzIjoxNjExNTUwMDUxLCJtZXRob2QiOiJzaWxlbnRJbnRlcmFjdGlvbiJ9
&nonce=6f25b4e0-71f7-4cde-abf4-cb5545d2507e
&client_info=1&x-client-SKU=MSAL.JS&x-client-Ver=1.4.4
&login_hint=admin@myemail.com
&client-request-id=a68ef0b3-111b-4b1c-a3e7-cdcb075516ca
&prompt=none&response_mode=fragment

编辑 3

我找到了这行代码

const GRAPH_ENDPOINT = 'https://graph.microsoft.com/v1.0/me';

getProfile() {
   this.http.get(GRAPH_ENDPOINT)
      .toPromise().then(profile => {
          this.profile = profile;
      });
 }

【问题讨论】:

  • 这是正常的。你得到的是 ms graph api 令牌,它只包含 ms graph api 的范围。一个令牌不能包含两个 API 的范围。如果需要获取自定义 api 的范围,则应该获取另一个令牌。

标签: angular azure-active-directory msal.js


【解决方案1】:

移动评论回答:

就像我在cmets中说的,你得到的是ms graph api token,它只包含ms graph api的范围。一个令牌不能包含两个 API 的范围。如果需要获取自定义 api 的范围,则应该获取另一个令牌。

访问令牌是根据你要访问的api audience颁发的,而且是唯一的!一个令牌只能有一个受众,并且您不能使用多个范围来请求访问令牌。

一句话总结:不能让一个access token同时包含API.AccessAPI.WriteUser.Readscp claim,因为这是两个完全不同的API的作用域。 p>

【讨论】:

  • 这在教程中并不明显。在我的示例中,观众值为"aud": "00000003-0000-0000-c000-000000000000"。这就是我定义权限authority: "https://login.microsoftonline.com/{tenantId}/" 的方式。查看更新
  • @Richard77 Audience "aud": "00000003-0000-0000-c000-000000000000" 实际上是ms graph api。我注意到您在代码中同时请求了两个令牌,但它只会返回您按顺序排列的第一个令牌,即 ms 图形令牌。
  • @Richard77 查看Microsoft Graph api的资源id:shawntabrizi.com/aad/…
  • 这是对图表发出请求的原因吗? authority: "https://login.microsoftonline.com/{tenantId}/",?请注意登录请求数组。它是空的,因为我注释掉了包含 ['user.read', 'openid', 'profile'] 的行。
  • @Richard77 不,这与您的请求网址无关。它与您的scope 有关。
猜你喜欢
  • 2020-04-25
  • 2023-03-23
  • 1970-01-01
  • 2016-11-08
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 2021-08-10
相关资源
最近更新 更多