发布于 2022-06-07 11:40:53
当您必须使用msal-react通过应用程序注销所有设备时,您肯定可以配置所需的应用程序,以便将登录应用程序的所有实例签出,如下所示:
MSAL.js v2提供了一种
logoutpopup
方法,用于清除浏览器存储中的缓存,并打开Azure Active Directory (Azure AD)登录页的弹出窗口。登录后,Azure将弹出重定向回您的应用程序,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://stackoverflow.com/questions/72493009
复制相似问题