Skip to content

ADAL Basics

Shane Oatman edited this page Jul 13, 2018 · 18 revisions

Get started

1. Get the project

In the build.gradle,

repositories {
    mavenCentral()
}
dependencies {
    // your dependencies here...
    implementation('com.microsoft.aad:adal:1.14.+') {
        // if your app includes android support libraries or Gson in its dependencies
        // exclude that groupId from ADAL's compile task by un-commenting the appropriate line below

        // exclude group: 'com.android.support'
        // exclude group: 'com.google.code.gson'
    }
}

2. Update the Android Manifest

In AndroidManifest.XML,

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
....
    <activity
        android:name="com.microsoft.aad.adal.AuthenticationActivity"
        android:label="@string/title_login_hello_app" >
    </activity>
....
<application/>

3. Initialize ADAL by creating an AuthenticationContext

 mAuthContext = new AuthenticationContext(getApplicationContext(), AUTHORITY_URL,false);

More information about the usage can be found at AuthenticationContext Wiki

4. Add an onActivityResult

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mAuthContext.onActivityResult(requestCode, resultCode, data);
    }

5. Get tokens

ADAL will manage all the token refresh and caching. To get the token you can call either of the methods below

  • acquireToken - Attempts to invoke an UI prompt with the user if needed to acquire tokens.The method can accessed like below:

    mAuthContext.acquireToken(MainActivity.this, RESOURCE_ID, CLIENT_ID, REDIRECT_URI,  PromptBehavior.Auto, 
    getAuthCallback());

    More information about the usage can be found at acquireToken Wiki

  • acquireTokenSilentSync - Acquires token from the cache without an UI prompt synchronously.The method can accessed like below:

    mAuthContext.acquireTokenSilentSync(RESOURCE_ID, CLIENT_ID, userId);
  • acquireTokenSilentASync - Acquires token from the cache without an UI prompt asynchronously.The method can accessed like below:

     mAuthContext.acquireTokenSilentAsync(RESOURCE_ID, CLIENT_ID, userId, getAuthCallback());

    More information about the usage can be found at acquireTokenSilent(...) Wiki

Clone this wiki locally