使用react-aad-msal在令牌到期后进行身份验证的重定向循环

时间:2020-07-16 04:41:11

标签: reactjs azure redirect msal

我试图用MSAL代替ADAL。用户第一次访问该应用程序时,它会正确进行身份验证,但是我遇到了这样的情况,即一段时间后我们的应用程序进入了重定向循环。我认为这是令牌过期的时间(我相信默认情况下为1小时)。有时候,当应用程序在一个小时后闲置时,就会发生这种情况,这就是为什么我认为这可能与获取新令牌的方式以及刷新浏览器窗口有关。

** AuthProvider.ts **

import { Configuration } from 'msal

// Msal Configurations
const config = {
   auth: {
      authority: 'https://login.microsoftonline.com/' + process.env.REACT_APP_AAD_TENANT,
      clientId: process.env.REACT_APP_AAD_CLIENT_ID,
      postLogoutRedirectUri: window.location.origin,
      redirectUri: window.location.origin,
      validateAuthority: true,
      navigateToLoginRequestUrl: false,
   },
   cache: {
      cacheLocation: 'sessionStorage', // This configures where your cache will be stored
      storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
   },
} as Configuration

// Authentication Parameters
const authenticationParameters = {
   scopes: ['openid', 'api://' + process.env.REACT_APP_AAD_SCOPES],
}

// Options
const options = {
   loginType: LoginType.Redirect,
   tokenRefreshUri: window.location.origin + '/auth.html',
}

export const authProvider = new MsalAuthProvider(config, authenticationParameters, options)

然后,当调用我们的api时,我以为我可以调用如下的getAccessToken方法,并且我会默默地收到一个有效的令牌,但是我最终会进入重定向循环。

我想知道为什么它在用户第一次访问应用程序时起作用,而在尝试刷新令牌时却不起作用。我认为Azure配置是正确的,因为它是第一次工作。可能是iframe /浏览器问题还是代码问题?

[![IdResponse tokenType undefined] [1]] [1] [1]:https://i.stack.imgur.com/6Vkwn.png

1 个答案:

答案 0 :(得分:-1)

要使用刷新令牌获取有效令牌,您需要调用msal.js令牌API AcquisitionTokenSilent。有关如何使用msal.js获取令牌的更多信息,请参见this document

您可以根据自己的情况参考msal.js sample