WPF / Winforms

Community maintained

Sample Project

Download a sample project specific to this tutorial configured with your Auth0 API Keys.

System Requirements
  • Microsoft Visual Studio 2017
  • .NET Framework 4.5.2
Show requirements

This tutorial explains how to integrate Auth0 with a WPF or Windows Forms application. The Auth0.OidcClient.WPF or Auth0.OidcClient.WinForms NuGet packages helps you authenticate users with any Auth0 supported identity provider.

Ensure Your Client is Configured Correctly

Before using the Auth0 OIDC Client, you will need to ensure that you have set the JsonWebToken Signature Algorithm to RS256 and have enabled the OIDC Conformant switch for your Client:

  1. Go to Dashboard > Clients
  2. Select your Client
  3. Go to Settings
  4. Click on Show Advanced Settings
  5. Click on the OAuth tab in Advanced Settings
  6. Change the JsonWebToken Signature Algorithm to RS256
  7. Ensure that the OIDC Conformant switch is enabled
  8. Click Save Changes

Please note that altering the signing algorithm for your client will immediately change the way your user's tokens are signed. This means that if you have already implemented JWT verification for your client somewhere, your tokens will not be verifiable until you update the logic to account for the new signing algorithm.

Install Auth0.OidcClient.WPF or Auth0.OidcClient.WinForms NuGet Package

Use the NuGet Package Manager (Tools -> Library Package Manager -> Package Manager Console) to install the Auth0.OidcClient.WPF or Auth0.OidcClient.WinForms package, depending on whether you are building a WPF or Windows Forms application:

Auth0.OidcClient.WPF

or

Auth0.OidcClient.WinForms

Set up the Auth0 Callback URL

Go to the Clients Settings section in the Auth0 dashboard and make sure that Allowed Callback URLs contains the following value:

https://YOUR_AUTH0_DOMAIN/mobile

Integration

To integrate Auth0 login into your application, simply instantiate an instance of the Auth0Client class, passing your Auth0 Domain and Client ID in the constructor.

// Form1.cs

using Auth0.OidcClient;

var client = new Auth0Client(new Auth0ClientOptions
{
    Domain = "YOUR_AUTH0_DOMAIN",
    ClientId = "YOUR_CLIENT_ID"
});

You can then call the LoginAsync method to log the user in:

var loginResult = await client.LoginAsync();

This will load Lock into a web view. If you want to customize Lock you need to enable the Custom Login Page from your hosted pages. Please refer to Lock documentation for available options.

Accessing the User's Information

The returned login result will indicate whether authentication was successful, and if so contain the tokens and claims of the user.

Authentication Error

You can check the IsError property of the result to see whether the login has failed. The ErrorMessage will contain more information regarding the error which occurred.

// Form1.cs

var loginResult = await client.LoginAsync();

if (loginResult.IsError)
{
    Debug.WriteLine($"An error occurred during login: {loginResult.Error}")
}

Accessing the tokens

On successful login, the login result will contain the id_token and access_token in the IdentityToken and AccessToken properties respectively.

// Form1.cs

var loginResult = await client.LoginAsync();

if (!loginResult.IsError)
{
    Debug.WriteLine($"id_token: {loginResult.IdentityToken}");
    Debug.WriteLine($"access_token: {loginResult.AccessToken}");
}

Obtaining the User Information

On successful login, the login result will contain the user information in the User property, which is a ClaimsPrincipal.

To obtain information about the user, you can query the claims. You can for example obtain the user's name and email address from the name and email claims:

// Form1.cs

if (!loginResult.IsError)
{
    Debug.WriteLine($"name: {loginResult.User.FindFirst(c => c.Type == "name")?.Value}");
    Debug.WriteLine($"email: {loginResult.User.FindFirst(c => c.Type == "email")?.Value}");
}

The exact claims returned will depend on the scopes that were requested. For more information see @scopes.

You can obtain a list of all the claims contained in the id_token by iterating through the Claims collection:

// Form1.cs

if (!loginResult.IsError)
{
    foreach (var claim in loginResult.User.Claims)
    {
        Debug.WriteLine($"{claim.Type} = {claim.Value}");
    }
}

More Information

For more information, please refer to the Auth0 OIDC Client Documentation.

Use Auth0 for FREECreate free Account