我开始研究一个项目,该项目将开发一个应用程序,使用多个ADFS来验证来自Web API的请求。
因此,我需要以某种方式使角度客户端从ADFS获取令牌,并在每次进入Web API时将其附加到http请求。
问题是我想知道是否有任何包已经这样做了?
我确实在这里找到了tutorial,但这不是我想要的。
答案 0 :(得分:3)
您应该尝试adal-angular5
在app.module.ts
import { HttpClient, HttpClientModule } from '@angular/common/http';
...
imports: [..., HttpClientModule ], // important! HttpClientModule replaces HttpModule
providers: [
Adal5Service,
{ provide: Adal5HTTPService, useFactory: Adal5HTTPService.factory, deps: [HttpClient, Adal5Service] } // // important! HttpClient replaces Http
]
实施例
import { Adal5HTTPService, Adal5Service } from 'adal-angular5';
...
export class HttpService {
constructor(
private adal5HttpService: Adal5HTTPService,
private adal5Service: Adal5Service) { }
public get(url: string): Observable<any> {
const options = this.prepareOptions();
return this.adal5HttpService.get(url, options)
}
private prepareOptions():any{
let headers = new HttpHeaders();
headers = headers
.set('Content-Type', 'application/json')
.set('Authorization', `Bearer ${this.adal5Service.userInfo.token}`);
return { headers };
}