Ionic 3 MSADAL

By Raddy in Ionic 3 ·

In this blog post I will explain how to get started with the Ionic Native plugin MS ADAL (Active Direcotry Authentication Library). The ADAL plugin provides easy to use authentication functionality for your Apache Cordova apps by taking advantage of Windows Server Active Directory and Windows Azure Active Directory. 

Before we get started there are a few important things that you need to know:

Let’s get started. 

1) Install Ionic 3

The easy way of installing ionic is by using npm. You can find the Ionic instructions here. If you don’t wish to read them just follow this tutorial, but I won’t be going into much detail as you should be already experienced at least a little bit with the framework.

Install Ionic

npm install -g ionic

Start an app

ionic start myApp tabs

Run your app

cd myApp
ionic serve

If at this point everything works – that’s great! In the next step we will have to install the MsADAL plugin.

2) Install MSAdal

First of all the full documentation can be found here. Make sure you read it or at least have a look as their installation guides are fairly clear.

To install the plugin we’ll use npm again.

  1. Install the Cordova and Ionic Native plugins:
ionic cordova plugin add cordova-plugin-ms-adal
npm install --save @ionic-native/ms-adal

Now that we have installed MSAdal we need to add it to our app.module.ts file: 

1- Start by importing the MSAdal

2 – Add MSAdal to your Providers

import { MSAdal } from'@ionic-native/ms-adal';

For this example I am just going to use the already generated Home page for the login. Of course you can generate a custom login page by using the generate command.

Open home.ts and insert the following code just like in the picture shown below:

Import Code:

import { MSAdal, AuthenticationContext, AuthenticationResult } from '@ionic-native/ms-adal';

Constructor Code:

private msAdal: MSAdal

Login function Code:

let authContext: AuthenticationContext = this.msAdal.createAuthenticationContext('');
authContext.acquireTokenAsync('', 'clienID-replace-this', 'http://localhost:8000','','')
.then((authResponse: AuthenticationResult) => {
console.log('Token is' , authResponse.accessToken);
console.log('Token will expire on', authResponse.expiresOn);
.catch((e: any) => console.log('Authentication failed', e));

3) Configuration – App Registration

This is where you can get your ClinetID. Go to and navigate to Azure Active Directory -> App registration. Once you are there click on the plus sign “+ New application registration“.

In the next step of creating your app registration, it’s very important to select Native.

Name: name your app

Application type: Native

Redirect URI: http://localhost:8000

Copy your ClientID and head back over to our Ionic App to replace mine under home.ts

4) Testing

It’s time to run our app on a simulator or real device. To do that run the following command for IOS or Android:

Once you login, MSADAL will return your accessToken.

Download source code

  1. Nirmalya Saha ·
    16th October 2019 8:05 am

    Thanks for the Post. But with this code, I have to login every time. How do I fetch the stored token and validate it?

    • Raddy ·
      16th October 2019 9:09 am

      I know that the Github project is now archived but you can still look at the documentation:

  2. Anton ·
    23rd October 2019 3:48 pm

    Raddy,good day! Thank you very much for post. Is this example work with Ionic 4 ? I had some issues with login every time and redirect back to my Android App.

    • Raddy ·
      23rd October 2019 4:01 pm

      Good day, Anton. Last time I tried was when Ionic 4 was in Beta version and I couldn't get it to work. Ionic was very buggy at that point and unfortunately I had to stick with Ionic 3. Since then I've heard that MSADAL is deprecated so now I am unsure if using the MSADAL Ionic plugin is the right way to go. It might be worth asking in the Ionic forum to see what people use. Let me know when you find out haha

  3. Lalit Kushwah ·
    16th November 2019 11:45 am

    Hi Raddy, I am getting eror MSAdalOriginal is not assignable to providers while adding MSAdal to the providers array of app.module.ts file

    • Raddy ·
      17th November 2019 12:09 am

      Did you install the MS-Adal plugin successfully? ionic cordova plugin add cordova-plugin-ms-adal npm install --save @ionic-native/ms-adal

  4. Bhoopal ·
    20th June 2020 11:51 am

    Hi I am using ionic 3 and I installed ms-adal plugin as your instruction, with all configuration I am able to redirect to microsoft login page after success login it redirect same microsoft login page I am not getting console.log('Token is' , authResponse.accessToken); console.log('Token will expire on', authResponse.expiresOn);

    • Raddy ·
      22nd June 2020 10:01 am

      That's really strange. So you are not getting Authentication failed?