我的应用组件:
export class AppComponent implements OnInit {
ngOnInit(): void {
this.adalService.handleWindowCallback();
this.adalService.getUser();
}
constructor(
private adalService: AdalService,
private secretService: AuthService,
private router: Router,
private appinsightsService: AppInsightsService,
@Inject('AiKey') private applicationInsightsKey: any
) {
this.appinsightsService.Init({
instrumentationKey: applicationInsightsKey
});
this.adalService.init(this.secretService.adalConfig);
}
}
我的登录组件:
export class LoginComponent {
model: any = {};
loading = false;
error = '';
constructor(
private router: Router,
private authenticationService: AdalService) {
if (this.authenticationService.userInfo.isAuthenticated) {
this.router.navigate(['/']);
} else {
this.authenticationService.login();
}
}
login() {
this.loading = true;
this.authenticationService.login();
}
}
我的路由器:
export const router: Routes = [
{ path: '', component: HomeComponent, canActivate: [LoggedInGuard] },
{
path: 'login',
component: LoginComponent
},
{
path: 'unauthorized',
component: UnauthorizedComponent
},
{ path: '**', redirectTo: '' }
];
export const routes: ModuleWithProviders = RouterModule.forRoot(router);
我的登录守卫:
@Injectable()
export class LoggedInGuard implements CanActivate {
constructor(private adalService: AdalService,
private router: Router) {
}
canActivate() {
if (this.adalService.userInfo.isAuthenticated) {
var roles = this.adalService.userInfo.profile.roles as Array<string>;
if (roles != undefined && roles.some(x => x === "user")) {
return true;
} else {
this.router.navigate(['/unauthorized']);
return false;
}
}
// not logged in so redirect to login page
this.router.navigate(['/login']);
return false;
}
}
我遇到的问题是我可以成功授权用户并使用受保护的API。一小时后,令牌过期,库会尝试在下一个网络请求中刷新它,它会尝试iframe刷新,但似乎不会使用新的访问令牌更新本地存储。