在多个应用程序中使用 Azure MSAL 进行身份验证,无需重定向

问题描述 投票:0回答:1

我只是想知道我正在朝着正确的方向前进,甚至可以使用 MSAL 库在 Azure 中进行身份验证。

我们在一个组织的同一租户下有多个单页应用程序。全部使用类似的子域,例如 a.service.com / b.service.com / c.service.com。

是否可以仅登录 Microsoft 帐户,并且当我们打开应用程序 a/b/c 时,我们将避免被重定向到 Microsoft 登录页面?

一般来说,它的工作原理就像我们被重定向到 Microsoft 页面,并且在 LocalStorage 中设置令牌(auth-/id-/refresh- tokens)。当我们返回应用程序时 - 我们仍然经过身份验证。但是,如果我们已经在其中任何一个应用程序中进行了身份验证,是否可以避免被重定向到每个应用程序的 Microsoft 身份验证页面?

应用程序之间的导航非常烦人。另外,一个应用程序似乎会将令牌/身份验证详细信息保留更长时间,并且重定向不会像其他应用程序那样频繁发生,但很难了解这里发生的情况。

非常感谢

我们只是在应用程序中使用 MsalGuard,并且在所有应用程序中使用具有相同 clientId/tenant 的 apiScope。

我应该手动运行 ssoSilent 还是 AFAIK Msal 总是尝试在后台默默地自动更新令牌?

angular azure authentication msal
1个回答
0
投票

MSAL Angular 公开了 3 种登录方法:loginPopup()、loginRedirect() 和 ssoSilent()。首先,设置默认交互类型 应用程序模块.ts:

[...]

如果您已经有一个经过身份验证的会话 服务器,您可以使用 ssoSilent() API 发出令牌请求 没有互动。您需要在请求中传递登录提示 对象以便以静默方式成功获取令牌。

export class AppComponent implements OnInit {

  constructor(
    private authService: MsalService,
  ) {}

  ngOnInit(): void {
    const silentRequest: SsoSilentRequest = {
      scopes: ["User.Read"],
      loginHint: "[email protected]"
    }

    this.authService.ssoSilent(silentRequest)
      .subscribe({
        next: (result: AuthenticationResult) => {
          console.log("SsoSilent succeeded!");
        }, 
        error: (error) => {
          this.authService.loginRedirect();
        }
      });
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.