Windows Universal App C#

Community maintained

Sample Project

Download this sample project configured with your Auth0 API Keys.

System Requirements
  • Microsoft Visual Studio 2017
  • Windows 10 SDK (10.0.10586.0)
Show requirements

This tutorial explains how to integrate the Auth0 OIDC Client with a Windows UWP (Universal Windows Platform) C# application. The Nuget package Auth0.OidcClient.UWP helps you authenticate users with any Auth0 supported identity provider.

Install the Auth0.OidcClient.UWP NuGet Package

Use the NuGet Package Manager Console (Tools -> NuGet Package Manager -> Package Manager Console) to install the Auth0.OidcClient.UWP package, running the command:

Install-Package Auth0.OidcClient.UWP

Set Up the Auth0 Callback URL

Go to the Client 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(
  "YOUR_AUTH0_DOMAIN",
  "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}");
}

[!Note] 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