Sign-in using an Android application in Azure Active Directory B2C

The Microsoft identity platform uses open standards such as OAuth2 and OpenID Connect. These standards allow you to leverage any library you wish to integrate with Azure Active Directory B2C. To help you use other libraries, you can use a walkthrough like this one to demonstrate how to configure 3rd party libraries to connect to the Microsoft identity platform. Most libraries that implement the RFC6749 OAuth2 spec can connect to the Microsoft Identity platform.

Warning

Microsoft does not provide fixes for 3rd party libraries and has not done a review of those libraries. This sample is using a 3rd party library called AppAuth that has been tested for compatibility in basic scenarios with the Azure AD B2C. Issues and feature requests should be directed to the library's open-source project. Please see this article for more information.

If you're new to OAuth2 or OpenID Connect much of this sample configuration may not make much sense to you. We recommend you look at a brief overview of the protocol we've documented here.

Get an Azure AD B2C directory

Before you can use Azure AD B2C, you must create a directory, or tenant. A directory is a container for all of your users, apps, groups, and more. If you don't have one already, create a B2C directory before you continue.

Create an application

Next, you need to create an app in your B2C directory. This gives Azure AD information that it needs to communicate securely with your app. To create a mobile app, follow these instructions. Be sure to:

  • Include a Native Client in the application.
  • Copy the Application ID that is assigned to your app. You will need this later.
  • Set up a native client Redirect URI (e.g. com.onmicrosoft.fabrikamb2c.exampleapp://oauth/redirect). You will also need this later.

Create your user flows

In Azure AD B2C, every user experience is defined by a user flow, which is a set of policies that control the behavior of Azure AD. This application requires a sign-in and sign-up user flow. When you create the user flow, be sure to:

  • Choose the Display name as a sign-up attribute in your user flow.
  • Choose the Display name and Object ID application claims in every user flow. You can choose other claims as well.
  • Copy the Name of each user flow after you create it. It should have the prefix b2c_1_. You'll need the user flow name later.

After you have created your user flows, you're ready to build your app.

Download the sample code

We have provided a working sample that uses AppAuth with Azure AD B2C on GitHub. You can download the code and run it. You can quickly get started with your own app using your own Azure AD B2C configuration by following the instructions in the README.md.

The sample is a modification of the sample provided by AppAuth. Please visit their page to learn more about AppAuth and its features.

Modifying your app to use Azure AD B2C with AppAuth

Note

AppAuth supports Android API 16 (Jellybean) and above. We recommend using API 23 and above.

Configuration

You can configure communication with Azure AD B2C by either specifying the discovery URI or by specifying both the authorization endpoint and token endpoint URIs. In either case, you will need the following information:

  • Tenant ID (e.g. contoso.onmicrosoft.com)
  • User flow name (e.g. B2C_1_SignUpIn)

If you choose to automatically discover the authorization and token endpoint URIs, you will need to fetch information from the discovery URI. The discovery URI can be generated by replacing the Tenant_ID and the Policy_Name in the following URL:

String mDiscoveryURI = "https://<Tenant_name>.b2clogin.com/<Tenant_ID>/v2.0/.well-known/openid-configuration?p=<Policy_Name>";

You can then acquire the authorization and token endpoint URIs and create an AuthorizationServiceConfiguration object by running the following:

final Uri issuerUri = Uri.parse(mDiscoveryURI);
AuthorizationServiceConfiguration config;

AuthorizationServiceConfiguration.fetchFromIssuer(
    issuerUri,
    new RetrieveConfigurationCallback() {
      @Override public void onFetchConfigurationCompleted(
          @Nullable AuthorizationServiceConfiguration serviceConfiguration,
          @Nullable AuthorizationException ex) {
        if (ex != null) {
            Log.w(TAG, "Failed to retrieve configuration for " + issuerUri, ex);
        } else {
            // service configuration retrieved, proceed to authorization...
        }
      }
  });

Instead of using discovery to obtain the authorization and token endpoint URIs, you can also specify them explicitly by replacing the Tenant_ID and the Policy_Name in the URL's below:

String mAuthEndpoint = "https://<Tenant_name>.b2clogin.com/<Tenant_ID>/oauth2/v2.0/authorize?p=<Policy_Name>";

String mTokenEndpoint = "https://<Tenant_name>.b2clogin.com/<Tenant_ID>/oauth2/v2.0/token?p=<Policy_Name>";

Run the following code to create your AuthorizationServiceConfiguration object:

AuthorizationServiceConfiguration config =
        new AuthorizationServiceConfiguration(name, mAuthEndpoint, mTokenEndpoint);

// perform the auth request...

Authorizing

After configuring or retrieving an authorization service configuration, an authorization request can be constructed. To create the request, you will need the following information:

  • Client ID (e.g. 00000000-0000-0000-0000-000000000000)
  • Redirect URI with a custom scheme (e.g. com.onmicrosoft.fabrikamb2c.exampleapp://oauthredirect)

Both items should have been saved when you were registering your app.

AuthorizationRequest req = new AuthorizationRequest.Builder(
    config,
    clientId,
    ResponseTypeValues.CODE,
    redirectUri)
    .build();

Please refer to the AppAuth guide on how to complete the rest of the process. If you need to quickly get started with a working app, check out our sample. Follow the steps in the README.md to enter your own Azure AD B2C configuration.