1

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

(1) I am not using Graph Api. After getting authenticated, I need to fetch the bearer token so that U can pass it in the HttpHeader while calling a rest api. (2) Also how can I authenticate against Azure B2C AD by providing a signing policy.
1) you could use other api by use change resource .you could fetch the access token in subscribe function , set header and make calls .2) please refer to this document
For (2), I need the Angular 4 SPA (Single Page Application) to authenticate against Azure B2C AD. I need to use the B2C AD 'Sign In' policy to authenticate by SPA. github.com/ranveeraggarwal/ng2-adal-QuickStart shows the authentication against Azure B2B AD but not for Azure B2B AD using a signin policy.
@Bobby , i would suggest you could start a new thread and add tag azure-ad-b2c since your question is not related to your original post .
@ Nan Yu - MSFT : This is noted, thanks. I have marked your reply as answer. Thanks for the help on this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.