首页 文章

MSAL acquireTokenSilent后跟acquireTokenPopup导致弹出窗口中出现错误请求

提问于
浏览
0

我们使用MSAL.js对Azure AD B2C实例的用户进行身份验证 . 用户可以使用本地帐户或使用其他Azure Active Directory实例中的凭据登录 .

登录后,我们的SPA使用acquireTokenSilent获取访问令牌,并获得后备至acquireTokenPopup .

我们注意到,当acquireTokenSilent超时时,仍然可以在后台检索令牌,并使用令牌更新应用程序本地存储 . 但是,在应用程序中,我们继续调用acquireTokenPopup . 用户在acquireTokenPopup中输入凭据后,弹出窗口显示“Bad Request” . 用户可以关闭弹出窗口,如果他们刷新应用程序,他们现在将登录 .

这种体验对我们的用户来说不是一次很棒的体验 .

只是想知道这是一个已知的问题还是预期的行为?

以下是我们代码的相关摘录 . 我们将UserAgentApplication包装在MsalAuthenticationManager对象中 .

function getMsalAuthenticationManager(authority: IMSALAuthorityConfig): IMsalAuthenticationManager {
  return new MsalAuthenticationManager(
    appConfig.msal.clientId,
    authority.signUpOrSignInAuthority,
    authority.passwordResetAuthority,
    {
      loadFrameTimeout: 15000,
      endPoints: endPointsMap,
      cacheLocation: appConfig.msal.cacheLocation // localStorage
    }
  );
}

// MsalAuthenticationManager constructor
  constructor(
    private applicationId: string,
    authority?: string,
    authorityForPasswordReset?: string,
    msalOptions?: IMsalOptions
  ) {
    var onTokenReceived = authorityForPasswordReset
      ? (errorDesc: string, token: string, error: string, tokenType: string) => {
          // When the user clicks on the forgot password link, the following error is returned to this app.
          // In this case we need to re-instantiate the UserAgentApplication object with the Password Reset policy.
          if (errorDesc && errorDesc.indexOf("AADB2C90118") > -1) {
            this._msal = new UserAgentApplication(
              applicationId,
              authorityForPasswordReset,
              onTokenReceived,
              msalOptions
            );

            this.signIn();
          }
        }
      : (errorDesc: string, token: string, error: string, tokenType: string) => {};

    this._msal = new UserAgentApplication(applicationId, authority, onTokenReceived, msalOptions);

    this.acquireToken = this.acquireToken.bind(this);
    this.signIn = this.signIn.bind(this);
    this.signOut = this.signOut.bind(this);
    this.getResourceForEndpoint = this.getResourceForEndpoint.bind(this); // Gets the scope for a particular endpoint
    this.acquireToken = this.acquireToken.bind(this);
  }

  public acquireToken(scopes: string[]): Promise<string> {
    return new Promise((resolve, reject) => {
      this._msal
        .acquireTokenSilent(scopes)
        .then((accessToken: string) => resolve(accessToken))
        .catch((acquireTokenSilentError: string) => {
          this._msal
            .acquireTokenPopup(scopes)
            .then((accessToken: string) => resolve(accessToken))
            .catch((acquireTokenPopupError: string) => reject(acquireTokenPopupError));
        });
    });
  }

1 回答

  • 0

    我有一个类似的问题,原因是令牌仍然存在但已过期,并且由于msal.js没有检查令牌过期,您将被视为已记录,但您的令牌实际上是无效的,您的带有持票人的httpRequests将失败,因为未经授权 . 你应该记录acquiretokenSilent错误并查找“AADB2C90077”错误,如果令牌过期,请调用logout() .

相关问题