Angular 4/Angular 2 Azure Active Directory 身份验证 - 获取令牌
Angular 4/Angular 2 Azure Active Directory Authentication - Fetch token
我需要针对 Azure Active Directory 验证 Angular 4/Angular 2 应用程序并获取令牌代码,因为我需要传递 Bearer 令牌来调用 REST API。
我使用了 https://github.com/ranveeraggarwal/ng2-adal-QuickStart 中的“ng2-adal”。
使用这个我可以成功验证。但是,我需要知道如何实现以下内容:
- 当我需要传递 Bearer 令牌来验证和调用 REST 服务时,如何获取字符串形式的令牌?
- 如何列出声明?
How can I fetch the Token as a string as I need to pass the Bearer token to authenticate & call a REST service?
您提供的代码示例实现了登录功能,如果您想获取访问令牌,可以使用 acquireToken(resource: string): Observable<string>;
函数来获取资源的访问令牌。例如,登录后,您可以参考下面的代码,点击按钮获取图 api 的令牌,然后您可以使用该令牌进行 api 调用:
import {Component} from '@angular/core';
import {AdalService} from 'ng2-adal/core';
@Component({
selector: 'home',
template: '<div protected><h1>This is the dashboard page.</h1><button (click)="logOut()">Logout</button><button (click)="callAPI()">callAPI</button></div>'
})
export class HomeComponent {
constructor(
private adalService: AdalService
) {
console.log('Entering home');
}
public logOut() {
this.adalService.logOut();
}
public callAPI() {
this.adalService.acquireToken("https://graph.microsoft.com").subscribe(p => {
console.log("Acquired token = " + p);
//then you could set Authorization Bearer header and call microsft graph api
}, (error => {
console.log(error);
}));
}
}
How do I list the claims
您要获取用户个人资料信息吗?如果是,您可以从 this.adalService.userInfo
我需要针对 Azure Active Directory 验证 Angular 4/Angular 2 应用程序并获取令牌代码,因为我需要传递 Bearer 令牌来调用 REST API。
我使用了 https://github.com/ranveeraggarwal/ng2-adal-QuickStart 中的“ng2-adal”。
使用这个我可以成功验证。但是,我需要知道如何实现以下内容:
- 当我需要传递 Bearer 令牌来验证和调用 REST 服务时,如何获取字符串形式的令牌?
- 如何列出声明?
How can I fetch the Token as a string as I need to pass the Bearer token to authenticate & call a REST service?
您提供的代码示例实现了登录功能,如果您想获取访问令牌,可以使用 acquireToken(resource: string): Observable<string>;
函数来获取资源的访问令牌。例如,登录后,您可以参考下面的代码,点击按钮获取图 api 的令牌,然后您可以使用该令牌进行 api 调用:
import {Component} from '@angular/core';
import {AdalService} from 'ng2-adal/core';
@Component({
selector: 'home',
template: '<div protected><h1>This is the dashboard page.</h1><button (click)="logOut()">Logout</button><button (click)="callAPI()">callAPI</button></div>'
})
export class HomeComponent {
constructor(
private adalService: AdalService
) {
console.log('Entering home');
}
public logOut() {
this.adalService.logOut();
}
public callAPI() {
this.adalService.acquireToken("https://graph.microsoft.com").subscribe(p => {
console.log("Acquired token = " + p);
//then you could set Authorization Bearer header and call microsft graph api
}, (error => {
console.log(error);
}));
}
}
How do I list the claims
您要获取用户个人资料信息吗?如果是,您可以从 this.adalService.userInfo