azure-active-directory Azure Active Directory B2C implementation using ADAL Cordova Plugin Azure Active Directory B2C implementation using ADAL Cordova Plugin


Refer the example here:

Azure AD B2C

Azure AD B2C is a cloud identity management solution for your web and mobile applications. It is a highly available global service that scales to hundreds of millions of identities.

Mobile app - ADAL plugin

Mobile app implementation uses ADAL Cordova Plugin Patch For B2C. This is a chopped version of Active Directory Authentication Library (ADAL) plugin for Apache Cordova apps, cordova-plugin-ms-adal that works with Azure AD B2C. The original cordova-plugin-ms-adal plugin provides easy to use authentication functionality for your Apache Cordova apps by taking advantage of Active Directory.

Find the angularjs/ionicframework example below

Install the dependencies:

cordova plugin add --save

bower install angular-jwt --save

Let us have a LoginController

.controller('LoginController', function($scope, $state, $ionicPopup, jwtHelper, AdalService) {

    $scope.login = function(){
        AdalService.login().then(function(authResponse) {

    $scope.logout = AdalService.logout;

    // Decode decode the token and diaplay the user details
    function getUserData(response) {
        var user = {};
        user.token = response.access_token || response.token;
        var data = jwtHelper.decodeToken(user.token);
        user.expires_in = new Date(response.expires * 1000) || response.expiresOn; =; = data.emails ? data.emails[0] : ''; = data.oid;
        return user;

    function displayUserDetails(user) {
        $scope.user = user;
            template: '<b>Email:</b> ' + + '<br> <b>Id:</b> <code>' + + '</code>'


Enter the Azure AD B2C settings here

.value('settings', {
    // ADAL-B2C configuration
    adalB2C: {
        tenantName: 'Enter your tenant name',
        clientId: 'Enter your client id',
        policy: 'Enter your policy name'

And here is the adal.service that implements Azure AD B2C using ADAL plugin

angular .module('azureADB2C') .service('AdalService', function($q, $http, settings) {

    var extraQueryParams = 'nux=1';
    var userId = null;
    var redirectUri = '';
    var authority = '' + settings.adalB2C.tenantName;
    var resourceUri = '';

    this.login = function() {
        var deferredLoginResponse = $q.defer();
        var authContext = new Microsoft.ADAL.AuthenticationContext(authority);
        // Attempt to authorize user silently
        authContext.acquireTokenSilentAsync(resourceUri, settings.adalB2C.clientId, userId, redirectUri, settings.adalB2C.policy)
            .then(function(authResponse) {
            }, function() {
                // We require user credentials so triggers authentication dialog
                authContext.acquireTokenAsync(resourceUri, settings.adalB2C.clientId, redirectUri, userId, extraQueryParams, settings.adalB2C.policy)
                    .then(function(authResponse) {
                    }, function(err) {
        return deferredLoginResponse.promise;

    this.logout = function() {
        // Step1: clear cache
        var authContext = new Microsoft.ADAL.AuthenticationContext(authority);

        // Step2: make XmlHttpRequest pointing to the sign out url
        return $ + '/oauth2/logout?post_logout_redirect_uri=' + redirectUri);