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

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

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

问题

以下是翻译好的内容:

  1. 我正在React应用中使用MSAL在登录后我使用以下代码获取详细信息-
  2. ```jsx
  3. const [userDetails, setUserDetails] = useState(null);
  4. useEffect(() => {
  5. instance
  6. .handleRedirectPromise()
  7. .then(() => {
  8. const currentUser = instance.getAllAccounts()[0];
  9. setUserDetails(currentUser);
  10. })
  11. .catch((error) => console.log(error));
  12. }, []);

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

  1. {
  2. "homeAccountId": "XX-X553252fedd35",
  3. "environment": "login.XX.net",
  4. "tenantId": "XX-63c7-XX-91c6-553252fedd35",
  5. "username": "X@XX.XX.com",
  6. "localAccountId": "XX-7e21-4730-XX-XX",
  7. "name": "XX XX",
  8. "idToken": "xcasdcasdf3adsfa4sdafsd43fadsf43asdfxx"
  9. "idTokenClaims": {
  10. XXXX: XXXX
  11. }
  12. }

重新加载之前-

屏幕截图

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

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

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

重新加载后-

屏幕截图

我正在使用以下npm包-

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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(() => {

  1. instance
  2. .handleRedirectPromise()
  3. .then(() =&gt; {
  4. const currentUser = instance.getAllAccounts()[0];
  5. setUserDetails(currentUser);
  6. })
  7. .catch((error) =&gt; console.log(error));

}, []);

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

{

  1. &quot;homeAccountId&quot;: &quot;XX-X553252fedd35&quot;,
  2. &quot;environment&quot;: &quot;login.XX.net&quot;,
  3. &quot;tenantId&quot;: &quot;XX-63c7-XX-91c6-553252fedd35&quot;,
  4. &quot;username&quot;: &quot;X@XX.XX.com&quot;,
  5. &quot;localAccountId&quot;: &quot;XX-7e21-4730-XX-XX&quot;,
  6. &quot;name&quot;: &quot;XX XX&quot;,
  7. &quot;idToken&quot;: &quot;xcasdcasdf3adsfa4sdafsd43fadsf43asdfxx&quot;
  8. &quot;idTokenClaims&quot;: {
  9. XXXX: XXXX
  10. }

}

  1. #### Before Reload-
  2. [ScreenShot](https://i.stack.imgur.com/1yyth.png)
  3. But when I reload the page, the IdToken got missing from this userDetails const.
  4. And in the console, I got this log message after reloading-

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

  1. #### After Reload-
  2. [ScreenShot](https://i.stack.imgur.com/ge5Ul.png)
  3. I am using these npm packages-

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

  1. I need to have the idToken for JWT authentication.
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. 你可以通过调用acquireTokenSilent来访问令牌:
  6. ```javascript
  7. instance.acquireTokenSilent(tokenRequest)
  8. .then((response) => {
  9. // 处理成功获取新令牌的情况
  10. // ...
  11. console.log(response);
  12. })
  13. .catch((error) => {
  14. // 处理令牌获取过程中出现的任何错误
  15. // ...
  16. });

tokenRequest示例:

  1. const tokenRequest = {
  2. scopes: ["openid"],
  3. };
英文:

You can access the token by calling acquireTokenSilent:

  1. instance.acquireTokenSilent(tokenRequest)
  2. .then((response) =&gt; {
  3. // Handle the successful acquisition of a new token
  4. // ...
  5. console.log(response);
  6. })
  7. .catch((error) =&gt; {
  8. // Handle any errors that occur during token acquisition
  9. // ...
  10. });

tokenRequest example:

  1. const tokenRequest = {
  2. scopes: [&quot;openid&quot;],
  3. };

答案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:

确定