错误版本的访问令牌(期望 V2,收到 V1)

Wrong version of access token (expect V2 , received V1)

我正在尝试使用@azure/msal-angular 从 Azure 活动目录注册用户,更准确地说,我尝试了以下 tutorial

这些是我对项目所做的更改

export function MSALInstanceFactory(): IPublicClientApplication {
  return new PublicClientApplication({
    auth: {
      clientId: 'my_real_client_id,
      redirectUri: 'http://localhost:4200',
      authority: 'https://login.microsoftonline.com/my_real_tenant_id',
      postLogoutRedirectUri: '/'
    },
    cache: {
      cacheLocation: BrowserCacheLocation.LocalStorage,
      storeAuthStateInCookie: isIE, // set to true for IE 11
    },
    system: {
      loggerOptions: {
        loggerCallback,
        logLevel: LogLevel.Info,
        piiLoggingEnabled: false
      }
    }
  });
}

  export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
  const protectedResourceMap = new Map<string, Array<string>>();
  protectedResourceMap.set('https://graph.microsoft.com/v1.0/me', ['user.read']);
  protectedResourceMap.set('http://localhost:5000/', ['profile']);

  return {
    interactionType: InteractionType.Redirect,
    protectedResourceMap
  };
}

问题是 MsalInterceptor 将 V1 令牌添加到 URL 以请求我的 API 期望 V2.

Azure 配置为 accessTokenAcceptedVersion:2

如果需要我可以提供更多信息

更新

在我的例子中,问题是由于指定的范围造成的,API for "user.read""profile " 需要 V1 accessToken

虽然你已经解决了问题,但我想澄清更多细节。

其实并不是权限“user.read”和“profile”需要V1 accessToken。实际原因是:

访问令牌版本由清单中 application/API 的配置决定。如果您想要 V2 访问令牌,则必须将清单的 accessTokenAcceptedVersion 部分更改为 2。这也是您在请求访问令牌时无法控制将从 客户端应用程序 获得的访问令牌版本的原因。

我们无法从 Microsoft Graph 端修改 accessTokenAcceptedVersion

所以结论是 MS Graph 的访问令牌和那些始终是 V1 访问令牌。