如何解决在 MSAL 中获取登录请求 URL
我目前在 Angular 9 应用程序中使用 MSAL.js 对 Azure AD B2C 租户进行身份验证。该应用程序的主页设置为一个登录页面,带有一个可触发以下 login()
函数的可点击按钮。其他路由使用 MsalGuard
进行保护。
export class LoginFormComponent implements OnInit {
constructor(
@Inject(MSAL_GUARD_CONFIG) private msalGuardConfig: MsalGuardConfiguration,private authService: MsalService,private router: Router) { }
public ngOnInit(): void {}
public login(): void {
if (this.msalGuardConfig.authRequest) {
this.authService.loginRedirect({ ...this.msalGuardConfig.authRequest } as RedirectRequest);
} else {
this.authService.loginRedirect();
}
}
}
在 app.module.ts
中,我已设置为当用户通过 Azure B2C 租户执行身份验证时,通过将 /login/auth-callback/
属性设置为 {{1 }},如下:
navigateToLoginRequestUrl
这样做的原因是我想在身份验证后执行一些额外的自定义检查。 false
包含以下组件,该组件使用 return new PublicClientApplication({
auth: {
clientId: environment.authentication.applicationId,redirectUri: '/login/auth-callback',postLogoutRedirectUri: '/',navigateToLoginRequestUrl: false
}
键 /login/auth-callback/
捕获原始用户的路线,然后执行到该路线的重定向。
sessionStorage
目前,此方法有效,但它看起来更像是一种解决方法,因为我正在读取触发 msal.[application-id].request.origin
后清除的会话存储密钥。除了上述之外,还有另一种(更好的)方法可以在 export class AuthCallbackComponent implements OnInit {
constructor(private authService: MsalService,private router: Router) { }
public ngOnInit(): void {
const redirectUrl = sessionStorage.getItem(`msal.${environment.authentication.applicationId}.request.origin`); // this will contain something like "http://localhost:4200/products"
this.authService.handleRedirectObservable().subscribe((res: AuthenticationResult) => {
if (res) {
// additional checks will be added here
this.authService.instance.setActiveAccount(res.account);
console.log(res);
this.router.navigate([redirectUrl.replace(environment.appUrl,'')]);
// "environment.appUrl" represents the Angular app URL - "http://localhost:4200";
// also aware that when logging in from the homepage the user is redirected back to the login page because of the above line,but that will be sorted out
}
},error => console.error(error));
}
}
之后检索用户访问的原始 URL 并将其重定向到?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。