如何在MSAL React应用程序中实现页面重新加载后保持IdToken?

huangapple go评论134阅读模式
英文:

How to maintain the IdToken after page reload in MSAL React App?

问题

以下是翻译好的内容:

我正在React应用中使用MSAL在登录后我使用以下代码获取详细信息-

```jsx
const [userDetails, setUserDetails] = useState(null);

useEffect(() => {

    instance

      .handleRedirectPromise()

      .then(() => {

        const currentUser = instance.getAllAccounts()[0];

        setUserDetails(currentUser);

      })

      .catch((error) => console.log(error));

  }, []);

在首次加载时,我将这些详细信息存储在userDetails常量中-

{

    "homeAccountId": "XX-X553252fedd35",

    "environment": "login.XX.net",

    "tenantId": "XX-63c7-XX-91c6-553252fedd35",

    "username": "X@XX.XX.com",

    "localAccountId": "XX-7e21-4730-XX-XX",

    "name": "XX XX",

    "idToken": "xcasdcasdf3adsfa4sdafsd43fadsf43asdfxx"

    "idTokenClaims": {

     XXXX: XXXX

    }
}

重新加载之前-

屏幕截图

但是当我重新加载页面时,userDetails中的IdToken消失了。

并且在控制台中,我在重新加载后收到以下日志消息-

@azure/msal-common@12.0.0 : Info - CacheManager:getIdToken - No token found

重新加载后-

屏幕截图

我正在使用以下npm包-

  "azure/msal-browser": "^2.34.0",
  "azure/msal-react": "^1.5.4",

我需要获得idToken以进行JWT身份验证。


<details>
<summary>英文:</summary>

I am using MSAL in React App. After the sign-in, I am getting the details using this code-

const [userDetails, setUserDetails] = useState(null);

useEffect(() => {

instance

  .handleRedirectPromise()

  .then(() =&gt; {

    const currentUser = instance.getAllAccounts()[0];

    setUserDetails(currentUser);

  })

  .catch((error) =&gt; console.log(error));

}, []);


In the first load, I am getting these details in the const userDetails-

{

&quot;homeAccountId&quot;: &quot;XX-X553252fedd35&quot;,

&quot;environment&quot;: &quot;login.XX.net&quot;,

&quot;tenantId&quot;: &quot;XX-63c7-XX-91c6-553252fedd35&quot;,

&quot;username&quot;: &quot;X@XX.XX.com&quot;,

&quot;localAccountId&quot;: &quot;XX-7e21-4730-XX-XX&quot;,

&quot;name&quot;: &quot;XX XX&quot;,

&quot;idToken&quot;: &quot;xcasdcasdf3adsfa4sdafsd43fadsf43asdfxx&quot;

&quot;idTokenClaims&quot;: {

 XXXX: XXXX

}

}


#### Before Reload-

[ScreenShot](https://i.stack.imgur.com/1yyth.png)

But when I reload the page, the IdToken got missing from this userDetails const.

And in the console, I got this log message after reloading-

@azure/msal-common@12.0.0 : Info - CacheManager:getIdToken - No token found


#### After Reload-
[ScreenShot](https://i.stack.imgur.com/ge5Ul.png)

I am using these npm packages-

"@azure/msal-browser": "^2.34.0",
"@azure/msal-react": "^1.5.4",


I need to have the idToken for JWT authentication.

</details>


# 答案1
**得分**: 0

你可以通过调用acquireTokenSilent来访问令牌:

```javascript
instance.acquireTokenSilent(tokenRequest)
    .then((response) => {
      // 处理成功获取新令牌的情况
      // ...
      console.log(response);
    })
    .catch((error) => {
      // 处理令牌获取过程中出现的任何错误
      // ...
    });

tokenRequest示例:

const tokenRequest = {
    scopes: ["openid"],
};
英文:

You can access the token by calling acquireTokenSilent:

instance.acquireTokenSilent(tokenRequest)
    .then((response) =&gt; {
      // Handle the successful acquisition of a new token
      // ...
      console.log(response);
    })
    .catch((error) =&gt; {
      // Handle any errors that occur during token acquisition
      // ...
    });  

tokenRequest example:

const tokenRequest = {
    scopes: [&quot;openid&quot;],
  };

答案2

得分: -1

为什么不将令牌保存在本地存储中?这样,即使在刷新页面后也可以保留它。

英文:

Why do not you save the token in localStorage. So, you can preserve it even on refresh?

huangapple
  • 本文由 发表于 2023年6月2日 14:57:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定