1

我正在使用带有 msal 的 JWT 令牌进行用户身份验证。用户登录后,我们将 JWT 令牌存储在 localStorage 中,并在注销时清除 localStorage 以及 msal loguut。

但我想强制注销浏览器上的用户已关闭。因此,一旦浏览器关闭,我必须清除 localStorage。

我尝试为此使用 onbeforeunload 和 onunload 方法,但是在页面刷新时也会调用此方法。- 尝试使用 sessionStorage 但这会导致用户登录其选项卡特定范围的每个选项卡 cos。

我尝试了以下代码

componentDidMount() {
   window.addEventListener("beforeunload",this.forceLogout,false)
}

componentWillUnmount() {
    window.removeEventListener("beforeunload",this.forceLogout,false)
}

forceLogout(){
  localStorage.clear();
}

注意:在 msal 登录重定向回应用程序后,由于使用了 HashRouter,我们会刷新页面

4

1 回答 1

1

If you use the session cache storage option in MSAL.js then the tokens will be cleared by the browser when it is closed.

https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/383c2e3089f139daaaaf7b81a80bc8c47b6c1273/lib/msal-core/README.md#cache-storage

However take care if you have multiple windows/tabs open of your app as the browser probably clear until all instances are closed.

于 2020-01-16T00:27:58.993 回答