我使用 Angular 4 和 ADAL 在我的网络应用程序中对用户进行身份验证,使用 ng2-adal 库,它是 adal.js 的包装器。
我遇到的问题如下: 所以 token 在一个时间限制后过期,我有一个 canActivate 路由保护来检查用户是否已通过身份验证。如果没有,它会将用户导航到登录页面。这是我的路线守卫的样子:
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AdalService } from 'ng2-adal/dist/core';
@Injectable()
export class RouteGuard implements CanActivate {
constructor(private router: Router, private adalService: AdalService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.adalService.userInfo.isAuthenticated) {
return true;
} else {
this.router.navigate(['/user-login']);
return false;
}
}
}
因此,只要 token 过期,用户就会被导航到登录页面,这对用户来说很烦人。有没有办法在 token 过期时更新 token ?
最佳答案
我想通了。这是我添加它的方式:
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AdalService } from 'ng2-adal/dist/core';
@Injectable()
export class RouteGuard implements CanActivate {
constructor(private router: Router, private adalService: AdalService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.adalService.userInfo.isAuthenticated) {
return true;
} else {
this.adalService.acquireToken(this.adalService.config.clientId).toPromise().then((data) => {
console.log('Generating a new authentication token.');
return true;
},
(error) => {
console.log('No user logged in.');
this.router.navigate(['/user-login']);
return false;
}
}
}
}
关于javascript - 刷新 id_token ADAL : Angular 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49949172/