msal - 等待 acquireTokenSilent

发布于 2025-01-16 02:46:05 字数 2238 浏览 4 评论 0原文

我正在我的 Angular 7 应用程序中实现 msal-v1,并且我想实现我的自己的拦截器,通过调用 获取访问令牌code>acquireTokenSilent 然后将令牌附加到 http 标头。

由于 acquireTokenSilent 是异步的,http 标头中的令牌始终为空,我需要应用程序等待检索令牌,然后设置标头>。

第一种方法

const acquireToken = async () => {
        let token = "";
        const authRequest = {
          scopes: ["user.read"],
        };
        try {
          const resp = await this.authService.acquireTokenSilent(authRequest);
          token = resp.accessToken;
        } catch (err) {
          if (err instanceof InteractionRequiredAuthError) {
            await this.authService.acquireTokenRedirect(authRequest);
          }
        }
        return token;
      };
      
      return next.handle(
        httpRequest.clone({
          setHeaders: {
            Authorization: `Bearer ${this.token}`,
          },
        })
      );

第二种方法

const authRequest = {
            scopes: ["clientID/.default"],
          };

      var token = this.getAuthenticationToken(authRequest);

      return next.handle(
        httpRequest.clone({
          setHeaders: {
            Authorization: `Bearer ${token}`,
          },
        })
      );
    }

------------------

public getAuthenticationToken(authRequest): Promise<string> {
    
    return this.authService
      .acquireTokenSilent(authRequest)
      .then((token) => {
        console.log("Got silent access token: ", token);
        return token.accessToken;
      })
      .catch((error) => {
        console.log("Could not silently retrieve token from storage.", error);
        return this.authService
          .acquireTokenPopup(authRequest)
          .then((token) => {
            console.log("Got popup access token: ", token);
            return token.accessToken;
          })
          .catch((error) => {
            console.log("Could not retrieve token from popup.", error);
            this.authService.acquireTokenRedirect(authRequest);
            return Promise.resolve("");
          });
      });
  }

但似乎没有任何效果,任何人都可以帮助我使 acquireTokenSilent 同步。
我采用这种方法的原因是因为我需要让用户在后台保持静默更新令牌的状态。

I'm implementing msal-v1 in my angular 7 application and I would like to implement my own interceptor where I get access token by calling acquireTokenSilent and then attaching the token to the http headers.

as the acquireTokenSilent is asynchronous, the token in the http headers is always empty, I need the application to wait until retrieve token and then set the headers.

First approach:

const acquireToken = async () => {
        let token = "";
        const authRequest = {
          scopes: ["user.read"],
        };
        try {
          const resp = await this.authService.acquireTokenSilent(authRequest);
          token = resp.accessToken;
        } catch (err) {
          if (err instanceof InteractionRequiredAuthError) {
            await this.authService.acquireTokenRedirect(authRequest);
          }
        }
        return token;
      };
      
      return next.handle(
        httpRequest.clone({
          setHeaders: {
            Authorization: `Bearer ${this.token}`,
          },
        })
      );

Second Approach:

const authRequest = {
            scopes: ["clientID/.default"],
          };

      var token = this.getAuthenticationToken(authRequest);

      return next.handle(
        httpRequest.clone({
          setHeaders: {
            Authorization: `Bearer ${token}`,
          },
        })
      );
    }

------------------

public getAuthenticationToken(authRequest): Promise<string> {
    
    return this.authService
      .acquireTokenSilent(authRequest)
      .then((token) => {
        console.log("Got silent access token: ", token);
        return token.accessToken;
      })
      .catch((error) => {
        console.log("Could not silently retrieve token from storage.", error);
        return this.authService
          .acquireTokenPopup(authRequest)
          .then((token) => {
            console.log("Got popup access token: ", token);
            return token.accessToken;
          })
          .catch((error) => {
            console.log("Could not retrieve token from popup.", error);
            this.authService.acquireTokenRedirect(authRequest);
            return Promise.resolve("");
          });
      });
  }

but nothing seems to work, can anyone help me to make the acquireTokenSilent as synchronous.
The reason I'm doing this approach is because I need to make the user to stay active with silent renew token in the background.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜肉鲜肉永远不皱 2025-01-23 02:46:05

我遇到了同样的问题,它对我有用,问题本身是拦截器调用是异步调用承诺的同步过程。

输入图片此处描述

此处代码

I went through the same problem, it worked for me, the problem itself is that the interceptor call being a synchronous process calling the promise asynchronously.

enter image description here

code here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文