Add Login to Your MAUI 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 MAUI application using the Auth0 SDKs for MAUI.

1

Configure Auth0

To use Auth0 services, you’ll 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 the project you are developing.

Configure an application

Use the interactive selector to create a new Auth0 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.

2

Install the Auth0 SDK

Auth0 provides a MAUI SDK to simplify the process of implementing Auth0 authentication in MAUI applications.

Use the NuGet Package Manager (Tools -> Library Package Manager -> Package Manager Console) to install the Auth0.OidcClient.MAUI package.

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

Install-Package Auth0.OidcClient.MAUI

Was this helpful?

/
dotnet add package Auth0.OidcClient.MAUI

Was this helpful?

/
3

Platform specific configuration

You need some platform-specific configuration to use the SDK with Android and Windows.

Android

Create a new Activity that extends WebAuthenticatorCallbackActivity:

[Activity(NoHistory = true, LaunchMode = LaunchMode.SingleTop, Exported = true)]
[IntentFilter(new[] { Intent.ActionView },
              Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
              DataScheme = CALLBACK_SCHEME)]
public class WebAuthenticatorActivity : Microsoft.Maui.Authentication.WebAuthenticatorCallbackActivity
{
    const string CALLBACK_SCHEME = "myapp";
}

Was this helpful?

/

The above activity will ensure the application can handle the myapp://callback URL when Auth0 redirects back to the Android application after logging in.

Windows

To make sure it can properly reactivate your application after being redirected back to Auth0, you need to do two things:

  • Add the corresponding protocol to the Package.appxmanifest. In this case, this is set to myapp, but you can change this to whatever you like (ensure to update all relevant Auth0 URLs as well).
    <Applications>
      <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="$targetentrypoint$">
        <Extensions>
          <uap:Extension Category="windows.protocol">
            <uap:Protocol Name="myapp"/>
          </uap:Extension>
        </Extensions>
      </Application>
    </Applications>

    Was this helpful?

    /
  • Call Activator.Default.CheckRedirectionActivation() in the Windows-specific App.xaml.cs file.
    public App()
    {
      if (Auth0.OidcClient.Platforms.Windows.Activator.Default.CheckRedirectionActivation())
        return;
    
      this.InitializeComponent();
    }

    Was this helpful?

    /
4

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, Client ID and the required Scopes. Additionally, you also need to configure the RedirectUri and PostLogoutRedirectUri to ensure Auth0 can redirect back to the application using the URL(s) configured.

using Auth0.OidcClient;

var client = new Auth0Client(new Auth0ClientOptions
{
    Domain = "{yourDomain}",
    ClientId = "{yourClientId}",
    RedirectUri = "myapp://callback",
    PostLogoutRedirectUri = "myapp://callback",
    Scope = "openid profile email"
});

Was this helpful?

/

By default, the SDK will leverage Chrome Custom Tabs for Android, ASWebAuthenticationSession for iOS and macOS and use your system's default browser on Windows.

Checkpoint

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

  • the Auth0Client is instantiated correctly in the MainPage.
  • your application is not throwing any errors related to Auth0
5

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 application redirects you to the Auth0 Universal Login page
  • you can log in or sign up
  • Auth0 redirects you to your application.
6

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 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
7

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 MAUI provides user information through the LoginResult.User property.

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.