【发布时间】:2017-12-19 10:23:27
【问题描述】:
我正在使用的应用程序通过 Angular 2+ 的 adal.js 端口实现 Azure Active Directory 授权,效果很好。
我们有一个AuthenticationGuard 来处理与我们自己的AdalService 之间的调用。 AdalService 有一个函数ensureAuthenticatedAsync 检查用户是否登录,如果没有,将他重定向到 AD 登录页面。
我们现在有了新的要求,即当没有可用的 AD 帐户时,我们可以使用自定义生成的令牌进行登录。经过一些修改,我的代码如下所示:
authentication.guard.ts
public canActivate(route: ActivatedRouteSnapshot, state RouterStateSnapshot) : boolean {
...
let params = route.queryParams;
let token = (params !== null && params['token'] !== undefined) ? params['token'] : null;
this.adalService.ensureAuthenticatedAsync(token);
...
return true;
}
adal.service.ts
private context: adal.AuthenticationContext;
public ensureAuthenticatedAsync(token: string | null = null) {
console.log('ensureAuthenticatedAsync', token);
this.isAuthenticated.subscribe(isAuthenticated => {
if (!isAuthenticated) {
console.log('not authenticated);
if (token === null) {
// forward to AAD login page -> this works perfectly
this.context.login();
} else {
...
console.log('accessToken before', this.accessToken);
this.customToken = token;
console.log('accessToken after', this.accessToken);
...
}
}
});
}
public get accessToken(): string {
console.log('customToken in get accessToken', this.customToken);
return (this.customToken === null) ? this.context.getCachedToken(this.adalClientId) : this.customToken;
}
public get isAuthenticated(): Observable<boolean> {
console.log('accessToken in get isAuthenticated', this.accessToken);
let isAuthenticated: boolean = (this.accessToken !== null);
return Observable.of(isAuthenticated);
}
app.component.ts
public ngOnInit(): void {
this.adalService.isAuthenticated.subscribe(isAuthenticated => {
if (isAuthenticated) {
// do some stuff here
}
});
}
app.routes.ts
export const ROUTES: Routes = [
{ path: 'Dashboard', component: DashboardComponent, canActivate: [AuthenticationGuard] },
{ path: 'Access-Denied', component: AccessDeniedComponent }
];
这是控制台中记录的内容:
authentication guard
customToken in get accessToken null
accessToken in get isAuthenticated null
customToken in get accessToken null
ensureAuthenticatedAsync 12345
not authenticated
customToken in get accessToken null
accessToken before null
customToken in get accessToken 12345
accessToken after 12345
customToken in get accessToken 12345
定期访问页面 (http://localhost:3000/Dashboard) 会正确触发重定向到 AD 登录页面,然后在登录后返回到我的应用程序。在那里,缓存的 AD 令牌被更改,并且 isAuthenticated 在订阅的任何地方都被触发(在我的例子中是 app.component.ts )。
但是,使用令牌 (http://localhost:3000/Dashboard/?token=12345) 访问页面不起作用。即使 customToken 使用参数中的值进行了修改,更改似乎也不会传播,并且 isAuthenticated 将 false 保留在订阅中。
我错过了什么吗?
【问题讨论】:
-
能否添加app.component.ts的相关代码以及customToken如何修改的例子?顺便说一句,isAuthenticated 返回一个 observable
似乎很奇怪,因为其中没有异步任务。 -
我添加了 app.component.ts 中的代码,我订阅了该属性。 customToken 在 adal.service.ts 中被修改(this.customToken = token)。
-
作为可观察对象:在此之前我有一个简单的布尔属性,从 adal 登录返回时它没有刷新值。我尝试了一个可观察的并且有效。
-
您能否添加您的路由配置和带有自定义令牌的路由示例,以了解如何调用身份验证保护?
-
我在 app.routes.ts 中添加了两个条目的示例,一个带有 AuthenticationGuard,另一个不带有 AuthenticationGuard。另外,我最后更改了描述以添加更多详细信息,包括路线示例。
标签: angular observable