1

我在反应应用程序中使用MSAL进行身份验证。我使用 localStorage 作为 UserAgentApplication 实例的缓存存储。loginPopup 方法提供一个有效期为一小时的 idToken。出于我的应用程序的目的,我想在它过期时静默刷新这个 idToken。根据所有文档,如果令牌过期,acquireTokenSilent 函数将自动刷新令牌,否则如果客户端 ID 作为范围参数传递,则将提供缓存的令牌。

但是,我在调用 acquireTokenSilent 函数时遇到了以下异常。

The cache contains multiple tokens satisfying the requirements. Call AcquireToken again providing more requirements like authority|multiple_matching_tokens_detected

令人惊讶的是,这个异常是在 chrome 上引发的,而不是在边缘浏览器上。文件系统中是否存储了任何浏览器特定的缓存?我已经清除了 chrome 中的 cookie 和本地存储数据,但没有任何帮助。

以下是一些负责任的代码片段:

login = () => {
return this.app.loginPopup(this.applicationConfig.graphScopes).then(
  idToken => {
    return idToken;
  },
  () => {
    return null;
  }
);

};

getToken = () => {
return this.app
  .acquireTokenSilent([this.applicationConfig.clientID], '', this.app.getUser())
  .then(
    idToken => {
      return idToken;
    },
    error => {
      console.log(
        "Error occured while loading silent token. Error - ",
        error
      );
    }
  );

};

首先,我尝试调用 getToken 函数,如果找不到令牌,那么我将用户重定向到使用登录函数的登录页面。请注意,我在这两种情况下都使用 idToken,但不是 accessToken。

非常感谢任何帮助以使这项工作在 chrome 中工作。

4

1 回答 1

1

试试这个代码,我认为它对你有用。

import { MsalService } from "@azure/msal-angular";

        msalConfig = {
            auth: {
                clientId: 'Your Client Id Here', //This is your client ID,
                authority: "https://login.microsoftonline.com/common", //This is your tenant info
            },
            cache: {
                cacheLocation: "localStorage",
                storeAuthStateInCookie: true
            }
        };
        myMSALObj = new Msal.UserAgentApplication(this.msalConfig);

        let requestObj = { scopes: ["user.read"] }
        let obj = this.myMSALObj;
        obj.loginPopup(requestObj).then(function (loginResponse) {
            obj.acquireTokenSilent(requestObj)
                .then(function (tokenResponse) {
                    console.log(tokenResponse.accessToken);
                    callmicrosoftLogin(tokenResponse.accessToken)
                }).catch(function (error) {
                    console.log(error);
                })
            obj.acquireTokenPopup(requestObj)
                .then(function (tokenResponse) {
                    console.log(tokenResponse.accessToken)
                }).catch(function (err) {
                    console.log(err);
                })
        }).catch(function (error) {
            console.log(error);
        });
于 2019-08-21T09:51:10.107 回答