Azure-Samples / active-directory-b2c-android-native-appauth

Demonstrates how you can use a third party library to build an Android application that authenticates Microsoft identity users to our B2C identity service and calls a web API using OAuth 2.0 access tokens.
http://aka.ms/aadb2c
Other
20 stars 12 forks source link
android appauth azure-active-directory azure-ad-b2c identity microsoft mobile

page_type: sample description: "This sample is a quickstart to help you get started with Azure AD B2C on Android using a 3rd party library called AppAuth." languages:

Integrate Azure AD B2C into an Android application

This sample demonstrates how to use Azure AD B2C using a 3rd party library called AppAuth. It has only been tested for compatibility in basic scenarios with Azure AD B2C. Issues and feature requests should be directed to the library's open-source project.

This sample is a quickstart to help you get started with Azure AD B2C on Android using a 3rd party library called AppAuth. The sample is already configured to use a demo environment and can be run simply by downloading the code and building the app on your machine. Follow the instructions below if you would like to use your own Azure AD B2C configuration.

This sample was adapted from the original Android AppAuth sample. For more details on how the sample and the library work, please look at the original sample.

Steps to Run

To use Azure AD B2C, you'll first need to create an Azure AD B2C tenant, register your application, and create some sign in and sign up experiences.

Setting up the Android App

  1. In Android Studio, click on "File"->"New"->"Import Project" and select the cloned folder. You will likely get a few errors and need to install some additional tools in Android Studio. Follow the prompts and let Android Studio update the local data.

    The app is already preconfigured to a demo Azure B2C tenant. At this point, you should be able to build and run the app. Follow the instructions below to configure the app with your own tenant information.

[!NOTE] developers using the Azure China Environment, MUST use .b2clogin.cn) authority, instead of login.chinacloudapi.cn.

The b2c_redirect_uri for China should use com.* and should be unique. This seems to be an issue with AppAuth accepting redirect Uris.

  1. Inside /app/res/values/idp_configs.xml, replace the following fields:

    • b2c_tenant: This is the name of your Azure AD B2C tenant
    • b2c_client_id: This is your Application ID, which can be found in the Azure Portal (under Application settings).
    • b2c_redirect_uri: This is your redirect URI, which can be found in the Azure Portal (under Application settings).
    • b2c_signupin_policy: This is the name of your Sign Up or Sign In policy.
  2. Inside '/app/build.gradle', replace the value for appAuthRedirectScheme. This should correspond to the scheme of the b2c_redirect_uri (without the /oauth/redirect).

  3. Go ahead and try the app. You'll be able to see your custom experience, sign up for an account, and sign in to an existing account. Upon completing the login process, you should see the types of tokens acquired.

Next Steps

Customize your user experience further by supporting more identity providers. Checkout the docs belows to learn how to add additional providers:

Microsoft

Facebook

Google

Amazon

LinkedIn

Questions & Issues

Please file any questions or problems with the sample as a github issue. You can also post on StackOverflow with the tag azure-ad-b2c.

This sample was built and tested with the Android Virtual Device Manager on versions 23-24 using Android Studio 2.2.3.

Acknowledgements

This sample was adapted from the Android AppAuth sample.