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, register an application in your Azure AD B2C tenant. This gives Azure AD the information it needs to communicate securely with your app.

To register an application in your Azure AD B2C tenant, you can use the current Applications experience, or our new unified App registrations (Preview) experience. Learn more about the new experience.

  1. Sign in to the Azure portal.
  2. Select the Directory + subscription filter in the top menu, and then select the directory that contains your Azure AD B2C tenant.
  3. In the left menu, select Azure AD B2C. Or, select All services and search for and select Azure AD B2C.
  4. Select Applications, and then select Add.
  5. Enter a name for the application. For example, nativeapp1.
  6. For Native client, select Yes.
  7. Enter a Custom Redirect URI with a unique scheme. For example, com.onmicrosoft.contosob2c.exampleapp://oauth/redirect. There are two important considerations when choosing a redirect URI:
    • Unique: The scheme of the redirect URI must be unique for every application. In the example com.onmicrosoft.contosob2c.exampleapp://oauth/redirect, com.onmicrosoft.contosob2c.exampleapp is the scheme. This pattern should be followed. If two applications share the same scheme, the user is given a choice to choose an application. If the user chooses incorrectly, the sign-in fails.
    • Complete: The redirect URI must have a both a scheme and a path. The path must contain at least one forward slash after the domain. For example, //oauth/ works while //oauth fails. Don't include special characters in the URI, for example, underscores.
  8. Select Create.

Record the Application (client) ID for use in a later step.

Also record your custom redirect URI for use in a later step. For example, com.onmicrosoft.contosob2c.exampleapp://oauth/redirect.

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 (APPLICATION ID) that you recorded earlier. For example, 00000000-0000-0000-0000-000000000000.
  • Custom Redirect URI that you recorded earlier. For example, com.onmicrosoft.contosob2c.exampleapp://oauth/redirect.

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.