如何使用msal.js强制从B2C刷新id_token并响应js

问题描述 投票:0回答:2

我在我的React项目中使用msal.js进行身份验证。在那,我有一个配置文件编辑选项。

修改个人资料的步骤

  1. 更新数据库
  2. 使用图形API更新B2C配置文件名称

因此,一旦我编辑了名称,B2C配置文件名称就会更新。但不是id_token。我想强制刷新id_token表B2C。 acquireTokenSilent方法始终从缓存中获取令牌。是否有任何方法可以强制应用程序从B2C获取新令牌?

这是我的acquireTokenSilent方法的代码

async acquireToken(request, redirect) {
      return msalApp
        .acquireTokenSilent(request)
        .then((loginResponse) => {
          console.log('gg');
          if (loginResponse) {
            console.log(loginResponse);
            this.setState({
              account: loginResponse.account,
              isAuthenticated: true,
              error: null,
            });
            return true;
          }
        })
        .catch((error) => {
          // Call acquireTokenPopup (popup window) in case of acquireTokenSilent failure
          // due to consent or interaction required ONLY
          if (requiresInteraction(error.errorCode)) {
            this.setState({
              isAuthenticated: false,
            });
            return redirect ? msalApp.acquireTokenRedirect(request) : this.onSignIn(redirect);
          }
          console.error('Non-interactive error:', error.errorCode);
          return false;
        });
    }
azure-active-directory microsoft-graph azure-ad-b2c msal msal.js
2个回答
0
投票

[在MSAL.js中检出方法的定义,以获取AcquisitionSilk方法:https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-core/src/UserAgentApplication.ts#L667

/**
 * Use this function to obtain a token before every call to the API / resource provider
 *
 * MSAL return's a cached token when available
 * Or it send's a request to the STS to obtain a new token using a hidden iframe.
 *
 * @param {@link AuthenticationParameters}
 *
 * To renew idToken, please pass clientId as the only scope in the Authentication Parameters
 * @returns {Promise.<AuthResponse>} - a promise that is fulfilled when this function has completed, or rejected if an error was raised. Returns the {@link AuthResponse} object
 *
 */
acquireTokenSilent(userRequest: AuthenticationParameters): Promise<AuthResponse> {

0
投票

由于您未使用B2C的配置文件编辑策略,因此会产生此结果。如果您清除了会话存储中的msal对象,则只会获得带有acquireTokenSilent的新令牌,否则,直到会话存储中的令牌过期,它都将用作缓存。

© www.soinside.com 2019 - 2024. All rights reserved.