1
votes

I need to authenticate Angular 4/Angular 2 app against Azure Active Directory and fetch token code as I need to pass the Bearer token to call a REST API.

I used 'ng2-adal' from https://github.com/ranveeraggarwal/ng2-adal-QuickStart.

Using this I can authenticate successfully. However, I need to know how to implement the below:

  1. How can I fetch the Token as a string as I need to pass the Bearer token to authenticate & call a REST service?
  2. How do I list the claims?
1

1 Answers

1
votes

How can I fetch the Token as a string as I need to pass the Bearer token to authenticate & call a REST service?

The code sample you provided implements sign in feature , if you want to get an access token , you could use acquireToken(resource: string): Observable<string>; function to acquire access token for resource . For example, after login, you could refer to the code below which clicks a button to acquire a token for the graph api, then you could use that token to make an api call :

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

Do you want to get the user profile information ? If yes , you could get that from this.adalService.userInfo