Add Login to Your .NET Android and iOS Application

Auth0 allows you to add authentication to almost any application type quickly. This guide demonstrates how to integrate Auth0, add authentication, and display user profile information in any .NET Android and iOS application using the Auth0 SDKs for Android and iOS.

To use this quickstart, you’ll need to:

  • Sign up for a free Auth0 account or log in to Auth0.
  • Have a working Android or iOS project using .NET 6 (or above) that you want to integrate with. Alternatively, you can view or download a sample application after logging in.
1

Configure Auth0

To use Auth0 services, you need to have an application set up in the Auth0 Dashboard. The Auth0 application is where you will configure how you want authentication to work for your project.

Configure an application

Use the interactive selector to create a new "Native Application", or select an existing application that represents the project you want to integrate with. Every application in Auth0 is assigned an alphanumeric, unique client ID that your application code will use to call Auth0 APIs through the SDK.

Any settings you configure using this quickstart will automatically update for your application in the Dashboard, which is where you can manage your applications in the future.

If you would rather explore a complete configuration, you can view a sample application instead.

Configure Callback URLs

A callback URL is a URL in your application that you would like Auth0 to redirect users to after they have authenticated. If not set, users will not be returned to your application after they log in.

Configure Logout URLs

A logout URL is a URL in your application that you would like Auth0 to redirect users to after they have logged out. If not set, users will not be able to log out from your application and will receive an error.

Lastly, be sure that the Application Type for your application is set to Native in the Application Settings.

2

Install the Auth0 SDK

Auth0 provides an Android and iOS SDK to simplify the process of implementing Auth0 authentication in .NET Android and iOS applications.

Use the NuGet Package Manager (Tools -> Library Package Manager -> Package Manager Console) to install the Auth0.OidcClient.AndroidX or Auth0.OidcClient.iOS package, depending on whether you are building an Android or iOS application.

Alternatively, you can use the Nuget Package Manager Console (Install-Package) or the dotnet CLI (dotnet add).

Install-Package Auth0.OidcClient.AndroidX
Install-Package Auth0.OidcClient.iOS

Was this helpful?

/
dotnet add Auth0.OidcClient.AndroidX
dotnet add Auth0.OidcClient.iOS

Was this helpful?

/
3

Instantiate the Auth0Client

To integrate Auth0 into your application, instantiate an instance of the Auth0Client class, passing an instance of Auth0ClientOptions that contains your Auth0 Domain and Client ID.

using Auth0.OidcClient;

var client = new Auth0Client(new Auth0ClientOptions
{
    Domain = "{yourDomain}",
    ClientId = "{yourDomain}"
}, this);

Was this helpful?

/

By default, the SDK will leverage Chrome Custom Tabs for Android and ASWebAuthenticationSession for iOS.

Checkpoint

Your Auth0Client should now be properly instantiated. Run your application to verify that:

  • the Auth0Client is instantiated correctly in the Activity (Android) or UIViewController (iOS)
  • your application is not throwing any errors related to Auth0
4

Configure Android

After a user successfully authenticates, they will be redirected to the callback URL you set up earlier in this quickstart.

To handle the callback on Android devices, you need to register an intent that handles this callback URL. An easy way to do this is to register the intent on the same activity from which you called the LoginAsync method to instantiate the authentication flow.

Ensure to replace YOUR_ANDROID_PACKAGE_NAME in the code sample with the actual Package Name for your application, such as com.mycompany.myapplication, and ensure that all the text for the DataScheme, DataHost, and DataPathPrefix is in lower case. Also, set LaunchMode = LaunchMode.SingleTask for the Activity, otherwise the system will create a new instance of the activity every time the Callback URL gets called.

Additionally, you need to handle the intent in the OnNewIntent event in your Activity class. You need to notify the Auth0 OIDC Client to finish the authentication flow by calling the Send method of the ActivityMediator singleton, passing along the URL that was sent in.

5

Configure iOS

After a user successfully authenticates, they will be redirected to the callback URL you set up earlier in this quickstart.

To handle the callback on iOS devices:

  • Open your application's Info.plist file in Visual Studio, and go to the Advanced tab.
  • Under URL Types, click the Add URL Type button
  • Set the Identifier as Auth0, the URL Schemes the same as your application's Bundle Identifier, and the Role as None

This is an example of the XML representation of your info.plist file after you have added the URL Type:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>None</string>
        <key>CFBundleURLName</key>
        <string>Auth0</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>YOUR_BUNDLE_IDENTIFIER</string>
        </array>
    </dict>
</array>

Was this helpful?

/

Additionally, you need to handle the Callback URL in the OpenUrl event in your AppDelegate class. You need to notify the Auth0 OIDC Client to finish the authentication flow by calling the Send method of the ActivityMediator singleton, passing along the URL that was sent in.

6

Add login to your application

Now that you have configured your Auth0 Application and the Auth0 SDK, you need to set up login for your project. To do this, you will use the SDK’s LoginAsync() method to create a login button that redirects users to the Auth0 Universal Login page.

var loginResult = await client.LoginAsync();

Was this helpful?

/

If there isn't any error, you can access the User, IdentityToken, AccessToken and RefreshToken on the LoginResult returned from LoginAsync().

Checkpoint

You should now be able to log in or sign up using a username and password.

Click the login button and verify that:

  • your Android or iOS application redirects you to the Auth0 Universal Login page
  • you can log in or sign up
  • Auth0 redirects you to your application.
7

Add logout to your application

Users who log in to your project will also need a way to log out. Create a logout button using the SDK’s LogoutAsync() method. When users log out, they will be redirected to your Auth0 logout endpoint, which will then immediately redirect them back to the logout URL you set up earlier in this quickstart.

await client.LogoutAsync();

Was this helpful?

/
Checkpoint

Run your application and click the logout button, verify that:

  • your Android or iOS application redirects you to the address you specified as one of the Allowed Logout URLs in your Application Settings
  • you are no longer logged in to your application
8

Show User Profile Information

Now that your users can log in and log out, you will likely want to be able to retrieve the profile information associated with authenticated users. For example, you may want to be able to display a logged-in user’s name or profile picture in your project.

The Auth0 SDK for Android and iOS provides user information through the LoginResult.User property.

if (loginResult.IsError == false)
{
    var user = loginResult.User;
    var name = user.FindFirst(c => c.Type == "name")?.Value;
    var email = user.FindFirst(c => c.Type == "email")?.Value;
    var picture = user.FindFirst(c => c.Type == "picture")?.Value;
}

Was this helpful?

/

Next Steps

Excellent work! If you made it this far, you should now have login, logout, and user profile information running in your application.

This concludes our quickstart tutorial, but there is so much more to explore. To learn more about what you can do with Auth0, check out:

Did it work?

Any suggestion or typo?

Edit on GitHub
Sign Up

Sign up for an or to your existing account to integrate directly with your own tenant.