WPF / Winforms

Community maintained

Sample Project

Download this sample project 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.

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.

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();

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.

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.

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:

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:

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