What are we going to do?
This is the third article in the series on how to integrate a web application that is built using Angular and ASP.NET core web APIs, with Azure Active Directory. You can see all the parts below,
- Part 1: Set up the Azure Active Directory
- Part 2: Set up Asp.net core web APIs to use Azure AD Authentication.
- Part 3: Set up an Angular application to use Azure AD Authentication.
This is Part 3, Set up an Angular application to use Azure AD Authentication. Here I will explain what code is required to integrate Azure AD with your Angular application.
Prerequisites
- Must have followed what we covered in Part 1: Set up the Azure Active Directory, Part 2: Set up Asp.net core web APIs to use Azure AD Authentication.
and should have the client and tenant Id created in the first part. - Basic knowledge of Angular.
- Must have an Angular application setup.
All right, now we are good to go. Let's get started.
Step 1
Install Microsoft adal package.
- npm install microsoft-adal-angular6
Step 2
Import the adal module in your application's app.module.
- MsAdalAngular6Module.forRoot({
- tenant: 'angular application tenant id',
- clientId: 'angular application client id',
- redirectUri: 'URI on which you want to redirect user after login',
- endpoints: {
- 'api application url': 'api application client id', // this is for feteching the access token
- },
- navigateToLoginRequestUrl: false,
- cacheLocation: '<localStorage / sessionStorage>',
- postLogoutRedirectUri: 'URI on which you want to redirect user after logout',
- }),
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
- import { AppRoutingModule } from './app-routing.module';
- import { AppComponent } from './app.component';
- import { MsAdalAngular6Module } from 'microsoft-adal-angular6';
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- BrowserModule,
- AppRoutingModule,
- MsAdalAngular6Module.forRoot({
- tenant: 'angular application tenant id',
- clientId: 'angular application client id',
- redirectUri: 'URI on which you want to redirect user after login',
- endpoints: {
- 'api application url': 'api application client id', // this is for feteching the access token
- },
- navigateToLoginRequestUrl: false,
- cacheLocation: '<localStorage / sessionStorage>',
- postLogoutRedirectUri: 'URI on which you want to redirect user after logout',
- }),
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule { }

We are done with configuration, now we are ready to use Azure AD's functionality. Following are some examples:
- import { Component } from '@angular/core';
- import { MsAdalAngular6Service } from 'microsoft-adal-angular6';
- import { Observable } from 'rxjs';
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
- })
- export class AppComponent {
- title = 'angular-client';
- accessToken: string;
- constructor(private adalService: MsAdalAngular6Service) {
- }
- login(): void {
- this.adalService.login();
- }
- logout(): void {
- this.adalService.logout();
- }
- getLoggedInUser(): any {
- return this.adalService.LoggedInUserEmail;
- }
- getAccessToken(): Observable<any> {
- return this.adalService.acquireToken('backend-api-uri');
- }
- getToken(): string {
- return this.adalService.accessToken;
- }
- }

raj kumarPosted Oct 12, 2022, 12:42 PM
Endpoints: { 'api application url': 'api application client id', // this is for feteching the access token }
raj kumarPosted Oct 12, 2022, 12:42 PM
Need pass any thing inside this one
Harkirat SinghPosted Jan 12, 2022, 3:35 PM
Hey how to dynamically route user after login to different redirectUri based on condition?
Karan ShahPosted Jul 25, 2020, 2:50 PM
In backend-api-uri,what do we need to mention?our .net core api endpoint or something else
Fabio CiconiPosted Jul 7, 2020, 7:25 PM
HI I am receiving the following error from backend: www-authenticate: Bearer error="invalid_token", error_description="The audience 'xxxxxxxxxxxxxxxxxxxxxxxx' is invalid"". The API only works when I use the FrontEnd's ClientId(angular) in the application.json, and it is supposed to be the API clientid, right?
Sandeep ThomasPosted Jun 9, 2020, 7:37 AM
Thats really nice. But I need my application to have Azure AD Single SignOn. It would be great if you can share some hints how to achieve that. Thanks a lot
Shubham KumarPosted Apr 7, 2020, 8:12 AM
Hey, how to implement ADAL along with Oauh2 implicit flow ?
deepak karmaPosted Mar 22, 2020, 10:39 AM
If i am keeping endpoints: { 'api application url': '', // this is for feteching the access token }, blank then still i am able to get token... so how it will authenticate webapi after login to angular?
deepak karmaPosted Mar 22, 2020, 10:38 AM
When i wanted to deploy my core web api then where i will deploy it?
Vitalii MonkoPosted Mar 13, 2020, 11:25 AM
Failed to load resource: the server responded with a status of 401 ().Bearer error="invalid_token", error_description="The audience 'XXXXXXXXXXXXX' is invalid". I have a token, but I got this error. Can you help with this? Thank you in advance!