React MSAL:如何从所有设备注销?

发布于 2025-02-04 02:13:37 字数 493 浏览 4 评论 0原文

我们有一个应用程序,其中我们使用 msal-reactct 用于login/logout。

是否有一种方法可以在登录时登录用户登录的所有设备?

不幸的是,官方文档关于这个。

我们也有登录的问题,因此用户必须在新的选项卡中再次登录(在打开不同的微观前端时 - 所有这些都使用相同的登录会话),尽管令牌存储在本地存储中。

希望有人可以在这里提供帮助或提供文档。

We have an application where we use msal-react for login/logout.

Is there a way to log out of all devices with which a user is logged in when logging out?

Unfortunately, nothing can be found in the official documentation about this.

We also have a problem with login, so that the user has to log in again in a new tab (when opening different micro frontends - which all use the same login session), although the token is stored in local storage.

Hope someone can help here or provide a documentation.

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

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

发布评论

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

评论(1

橙幽之幻 2025-02-11 02:13:37

当您必须使用 msal-react 的应用程序注销所有设备时,您可以肯定地配置所需的应用程序以登录所有在应用程序中的登录实例,如下所示:

msal.js v2提供logoutPopup方法,该方法清除了浏览器存储中的高速缓存,并将弹出窗口打开到Azure Active Directory(Azure AD)登录页面。登录后,Azure AD将弹出窗口重定向到您的应用程序,MSAL.js将关闭弹出窗口。

通过这种方式,您可以确保您的React应用程序已从Azure Active Directory登录,并且所有设备都将用户登录的信息汇总。

使用弹出窗口签名:

 import { useMsal, AuthenticatedTemplate, UnauthenticatedTemplate } from "@azure/msal-react";

 function signOutClickHandler(instance) {
 const logoutRequest = {
    account: instance.getAccountByHomeId(homeAccountId),
    mainWindowRedirectUri: "your_app_main_window_redirect_uri",
    postLogoutRedirectUri: "your_app_logout_redirect_uri"
  }
  instance.logoutPopup(logoutRequest);
 }

// SignOutButton Component returns a button that invokes a popup logout when clicked
  function SignOutButton() {
// useMsal hook will return the PublicClientApplication instance you provided to MsalProvider
const { instance } = useMsal();

return <button onClick={() => signOutClickHandler(instance)}>Sign Out</button>
 };

  // Remember that MsalProvider must be rendered somewhere higher up in the component tree
  function App() {
   return (
    <>
        <AuthenticatedTemplate>
            <p>This will only render if a user is signed-in.</p>
            <SignOutButton />
        </AuthenticatedTemplate>
        <UnauthenticatedTemplate>
            <p>This will only render if a user is not signed-in.</p>
        </UnauthenticatedTemplate>
       </>
      )
    }

类似地,如果您使用重定向方法参考用于从应用程序中登录的方法,则必须通过设置Postlogoutredirecturi来配置其在登录后应重定向到的URI。该URI应在您的申请注册中注册为重定向URI。

有关更多信息,请参考下面的文档链接:

https://learn.microsoft.com/en-us/azure/active-directory/directory/develip/scenario-scenario-spa-sign-sign-sign-sign-sign-sign-sign-sign-sign-sign-sign-sign-在?tabs = react#签名 - with-a-pop-up-window

When you have to logout of all devices through an application using msal-react, then you can surely configure the required application for signing out of all instances of logged in application as below:

MSAL.js v2 provides a logoutpopup method that clears the cache in browser storage and opens a pop-up window to the Azure Active Directory (Azure AD) sign-out page. After sign-out, Azure AD redirects the pop-up back to your application and MSAL.js will close the pop-up.

In this way, you can ensure that your react application has logged out of Azure Active Directory and all devices reciprocate the information that the user has logged out.

Signing out with a pop-up window:

 import { useMsal, AuthenticatedTemplate, UnauthenticatedTemplate } from "@azure/msal-react";

 function signOutClickHandler(instance) {
 const logoutRequest = {
    account: instance.getAccountByHomeId(homeAccountId),
    mainWindowRedirectUri: "your_app_main_window_redirect_uri",
    postLogoutRedirectUri: "your_app_logout_redirect_uri"
  }
  instance.logoutPopup(logoutRequest);
 }

// SignOutButton Component returns a button that invokes a popup logout when clicked
  function SignOutButton() {
// useMsal hook will return the PublicClientApplication instance you provided to MsalProvider
const { instance } = useMsal();

return <button onClick={() => signOutClickHandler(instance)}>Sign Out</button>
 };

  // Remember that MsalProvider must be rendered somewhere higher up in the component tree
  function App() {
   return (
    <>
        <AuthenticatedTemplate>
            <p>This will only render if a user is signed-in.</p>
            <SignOutButton />
        </AuthenticatedTemplate>
        <UnauthenticatedTemplate>
            <p>This will only render if a user is not signed-in.</p>
        </UnauthenticatedTemplate>
       </>
      )
    }

Similarly, if you refer to the method for signing out of the application using the redirect method, you will have to configure the URI to which it should redirect after sign-out by setting postLogoutRedirectUri. This URI should be registered as a redirect URI in your application registration.

For more information, kindly refer to the documentation link below:

https://learn.microsoft.com/en-us/azure/active-directory/develop/scenario-spa-sign-in?tabs=react#sign-out-with-a-pop-up-window

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