Edit

Share via


Sign in users in sample Android (Kotlin) app by using native authentication

Applies to: White circle with a gray X symbol. Workforce tenants Green circle with a white check mark symbol. External tenants (learn more)

In this quickstart you learn how to run an Android sample application that demonstrates sign-up, sign in, sign out, and password reset scenarios using Microsoft Entra's native authentication.

Prerequisites

Enable public client and native authentication flows

To specify that this app is a public client and can use native authentication, enable public client and native authentication flows:

  1. From the app registrations page, select the app registration for which you want to enable public client and native authentication flows.
  2. Under Manage, select Authentication.
  3. Under Advanced settings, allow public client flows:
    1. For Enable the following mobile and desktop flows select Yes.
    2. For Enable native authentication, select Yes.
  4. Select Save button.

Clone sample Android mobile application

  1. Open Terminal and navigate to a directory where you want to keep the code.

  2. Clone the application from GitHub by running the following command:

    git clone https://github.com/Azure-Samples/ms-identity-ciam-native-auth-android-sample 
    

Configure the sample Android mobile application

  1. In Android Studio, open the project that you cloned.

  2. Open app/src/main/res/raw/native_auth_sample_app_config.json file.

  3. Find the placeholder:

    • Enter_the_Application_Id_Here and replace it with the Application (client) ID of the app you registered earlier.
    • Enter_the_Tenant_Subdomain_Here and replace it with the Directory (tenant) subdomain. For example, if your tenant primary domain is contoso.onmicrosoft.com, use contoso. If you don't know your tenant subdomain, learn how to read your tenant details.

You've now configured the app and it's ready to run.

Run and test the sample Android mobile application

To build and run your app, follow these steps:

  1. In the toolbar, select your app from the run configurations menu.

  2. In the target device menu, select the device that you want to run your app on.

    If you don't have any devices configured, you need to either create an Android Virtual Device to use the Android Emulator or connect a physical Android device.

  3. Select the Run button. The app opens the Email & OTP screen.

    Screenshot of user prompt to enter email in Android application.

  4. Enter a valid email address and select then Sign up button. The app opens the submit code screen and you receive an OTP code in the email address.

    Screenshot of user prompt to enter one-time passcode in Android application.

  5. Enter the OTP code that you receive in the email inbox and select Next. If the sign-up is successful, the app signs you in automatically. If you don't receive the OTP code in your email inbox, you can resend it after a while by selecting Resend Passcode.

  6. To sign out, select the Sign out button.

Other scenarios that this sample supports

This sample app also supports the following authentication flows:

  • Email + password covers sign-in or sign-up flows with an email with password.
  • Email + password sign-up with user attributes covers sign-up with email and password, and submitting user attributes.
  • Password reset covers self-service password reset (SSPR).
  • Access Protected API covers call a protected API after the user successfully signs up or signs in and acquires an access token.
  • Fallback to web browser covers the use the browser-based authentication as a fallback mechanism when the user can't complete authentication through native authentication for whatever reason.

Test email with password flow

In this section, you test email with password flow, with its variants such as, email with password sign-up with user attributes and SSPR:

  1. Use the steps in create a user flow to create a new user flow, but this time select Email with password as your authentication method. You need to configure Country/Region and City as the user attributes. Alternatively, you can modify the existing user flow to use Email with password (Select External Identities > User flows > SignInSignUpSample > Identity providers > Email with password > Save).

  2. Use the steps in associate the application with the new user flow to add an app to your new user flow.

  3. Run the sample app, then select the ellipsis menu (...) to open more options.

  4. Select the scenario you want to test, such as Email + password or Email + password sign-up with user attributes or Password reset, then follow the prompts. To test Password reset, you need to first sign up a user, and enable email one-time passcode for all users in your tenant.

Test call a protected API flow

Use the steps in Call a protected web API in a sample Android mobile app by using native authentication to call a protected web API from a sample Android mobile app.

Next steps