使用 Azure AD 和 MSAL 的 SPA 应用程序之间的 SSO

Pau*_*aul 4 azure single-sign-on azure-active-directory azure-ad-msal msal.js

我们有一堆单页应用程序并部署它们,例如 appA.xyz-corp.com 和 appB.xyz-corp.com。我们如何配置 Msal javascript,以便用户不必登录每个应用程序。我们尝试了此处提到的方法https://github.com/AzureAD/microsoft-authentication-library-for-js/wiki/Sso。但是,会话存储存储每个域“appA.xyz-corp.com”的信息,而不是使用子域“xyz.com”。对于使用 msal.js 进行多个单页应用程序以及用户跨应用程序无缝登录的最佳实践,我们将不胜感激。

Ale*_*AIT 7

您提到的 wiki也有一些关于这种具有多个域的场景的信息。本质上,您需要捕获首选用户名并将其与登录请求一起发送。要同时捕获所有应用程序的用户名,我建议将其存储在所有应用程序都知道的域范围 cookie 中。

 // Store the username after login
 document.cookie = "msal_username=Paul@xyz-corp.com;domain=.xyz-corp.com;path=/"

 // use the username
 var username = getCookieByName("msal_username"); // find some code to do that
 userAgentApplication.loginRedirect(scopes, "&login_hint=" + username);
Run Code Online (Sandbox Code Playgroud)

这样做的缺点是您需要在所有应用程序中实现它。

不同领域的应用

当应用程序托管在不同的域上时,域 B 中的 MSAL.js 无法访问域 A 上缓存的令牌。

自动选择 Azure AD 上的帐户

...

使用登录提示

如果未配置 SID 声明或需要在交互式身份验证调用中绕过帐户选择提示,则可以通过在 MSAL.js 交互式方法(loginPopup、loginRedirect、acquireTokenPopup 和 acquireTokenRedirect)中提供 login_hint 和(可选)domain_hint 作为 extraQueryParameters 来实现此目的)。例如:

userAgentApplication.loginRedirect(scopes, "&login_hint=<preferred_username>&domain_hint=organizations");

您可以通过读取用户 ID 令牌中返回的声明来获取 login_hint 和 domain_hint 的值。

login_hint 应设置为 ID 令牌中的 Preferred_username 声明。