我有一个应用程序,如果他们已经通过我们的任何其他应用程序登录到提供我身份验证的IdentityServer项目,我需要使用Auth-Guard来执行静默续订呼叫,以允许他们通过。基本上,我们希望在应用程序之间有一个无缝的登录流。
目前,我处理这个问题的方式是在每个页面上都有一个身份验证保护,对于某些不需要身份验证的页面,但可以使用它来处理对服务器的角色请求,我会检查它,执行静默续订以防万一,然后获取角色,如果它们通过身份验证,则仍然返回true。
我已经把它分解成了一个测试auth-guard,去掉了角色部分,并试图找出让它工作的最佳方法。目前,似乎存在一个问题,如果我登录到IdentityServer,而不是客户端应用程序,身份验证保护永远不会完成可观察对象中的过程。
代码如下:
AuthGuard:
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot,
): Observable<boolean> | Promise<boolean | UrlTree> | boolean {
this.spinner.start();//this blocks the screen until it is done, it is what told me that nothing
//else was getting hit
return this.oidcAuthService.isUserLoggedIn.pipe(
delay(1000),
map(loggedIn => {
if (!loggedIn) {
this.oidcAuthService.renewToken().then(user => {
if (user && !user.expired) {
return true;
} else {
return this.accessDenied();
}
}).catch(error => {
if (error instanceof HttpErrorResponse) {
if (error.message === 'login_required') {
return this.accessDenied();
}
return this.accessDenied();
}
})
} else {
return true;
}
}),
);
}
private accessDenied() {
window.alert("You don't have permission to view this page");
this.router.navigate(['/']);
return false;
}..。
OidcAuthService:
get isUserLoggedIn() {
return this._isUserLoggedIn.asObservable();
}问题是,如果客户端未登录,而服务器已登录,我似乎无法让isUserLogged检查中的代码运行。
发布于 2020-12-24 00:58:00
不要使用.pipe()而要使用.subscribe()
https://stackoverflow.com/questions/65425439
复制相似问题