Active Directory Authentication Library made easy
Let’s see how we can make the Active Directory authentication incredibly easy for your Xamarin projects.
To help you to speed up your Xamarin development, I’ve created a set of plugins, one of them is MTADAL. Thanks to this plugin you can authenticate users in your projects with a single line of code.
A couple of useful link you can find useful
Nuget link: https://www.nuget.org/packages/MarcTron.ADAL\
Project website: https://www.xamarinexpert.it/Plugin/MTADALh
To report any issue: https://bitbucket.org/marcojak81/mtadal
And now let’s see how to integrate the plugin inside your Xamarin Forms solution.
Add the Active Directory Plugin
First of all we need to install the plugin. To do that, do a right-click on your solution and click on “Manage NuGet Packages for Solution…”

Manage the Nuget packages
Now search the package MarcTron.ADAL, click on it and remember to select all your projects (.Net Standard project + all the main application projects).

The Plugin will take care to install for you also the Nuget package Microsoft.IdentityModel.Clients.ActiveDirectory so you need to accept the Microsoft License.
If everything worked as expected, you will see the version of the plugin next to each of the projects you have selected in the previous step.
Now it’s time to try the plugin to see how easy it is.
Authenticate your users
Inside the Button_OnClicked method, you can see the only line of code you need to authenticate the user:
AuthenticationResult data = await MTADAL.Current.Authenticate(Authority, GraphResourceUri, ClientId, ReturnUri);
using System;
using MarcTron.Plugin.ADAL;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Xamarin.Forms;
namespace YOURNAMESPACE
{
public partial class MainPage : ContentPage
{
public const string ClientId = "YOUR CLIENT ID";
public static readonly string ReturnUri = "YOUR RETURN URI";
public const string GraphResourceUri = "YOUR Graph Resource Uri";
private const string AadInstance = "YOUR AAD Instance";
private const string Tenant = "YOUR TENANT";
public static readonly string Authority = $"{AadInstance}{Tenant}";
public MainPage()
{
InitializeComponent();
}
private async void Button_OnClicked(object sender, EventArgs e)
{
AuthenticationResult data = await MTADAL.Current.Authenticate(Authority, GraphResourceUri, ClientId, ReturnUri);
if (data != null)
await DisplayAlert("MTADAL", "Hello " + data.UserInfo.GivenName, "Ok");
}
}
}
Of course you need to set Authority, GraphResourceUri, ClientId, ReturnUri
according to your Active Directory credentials.
If we run this simple code and we insert the correct email and password this is what we seeAuthentication completed! And with only a single line of code!
USAGE ON ANDROID
To use this plugin on Android, add the following line inside the OnCreate method of your MainActivity:
MTADAL.Current.Init(this);
Add that line just after the Xamarin Forms initialization
Xamarin.Forms.Forms.Init(this, bundle);
What do you think? Add your comment at the end of the page.
October 20, 2020 @ 10:58 pm
You’re link to the example “how to” project is currently broken. Does it still exist? Thanks