【问题标题】:How to get access token with MSAL如何使用 MSAL 获取访问令牌
【发布时间】:2021-12-02 23:12:08
【问题描述】:

我将大量教程和文档拼凑在一起,以便在我的 JavaScript 代码中使用 MSAL 获取访问令牌。这是我的研究结果。

【问题讨论】:

    标签: msal


    【解决方案1】:
    1. npm install @azure/msal

    2. 从@azure/msal 导入必要的类

          import {
            UserAgentApplication,
            AuthenticationParameters,
            Configuration,
          } from "@azure/msal";
      
      
    3. 制作 msal 对象

        const config: Configuration = {
          auth: {
            clientId: <client id - your app's client id>,
            authority: `https://login.microsoftonline.com/<tenantid>`,
            redirectUri: <the redirect Uri>,
          },
        };
      
        const params: AuthenticationParameters = {
          authority: `https://login.microsoftonline.com/${Tenantid}`,
          scopes: [`${AppIDUri}/user_impersonation`],  <-- the API that you're trying to call
        };
      
        const myMSAL = new UserAgentApplication(config);
      
    4. 获取访问令牌

       try {
        const login = await myMSAL.acquireTokenSilent(params);
        return login.accessToken;
      } catch (error) {
        await myMSAL.loginPopup(params);
        const login = await myMSAL.acquireTokenSilent(params);
        return login.accessToken;
      }
      

    参考资料:

    https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-acquire-cache-tokens

    Azure/Msal authentication inside PowerApp Component Framework returns AADSTS50177 error

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-15
      • 2021-11-15
      • 2021-10-03
      • 1970-01-01
      • 2019-04-18
      • 2020-09-15
      • 1970-01-01
      • 2019-04-11
      相关资源
      最近更新 更多