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

问题描述

我正在尝试使用 @azure/msal-angular 从 Azure Active 目录注册用户,更准确地说,我尝试了以下 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 令牌添加到我的 API 请求的 URL 中,该请求需要 V2

Azure 配置为 accesstokenAcceptedVersion:2

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

更新

就我而言,问题是由于指定的范围造成的,"user.read""profile" 的 API 都需要 V1 accesstoken

解决方法

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

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

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

我们无法从 Microsoft Graph 端修改 accessTokenAcceptedVersion

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