Windows Universal App C#

Community maintained

Sample Project

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

System Requirements
  • Microsoft Visual Studio 2017
  • Windows 10 SDK (10.0.10586.0)
  • Auth0.OidcClient.UWP 1.0.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.

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

For UWP applications, the callback URL needs to be in the format ms-app://SID, where SID is the Package SID for your application. Assuming you have associated your application with and application on the Windows Store, you can go to the Windows Developer Centre, go to the settings for your application, and then go to the App management > App identity section, where you will see the Package SID listed.

Alternatively - or if you have not associated your application with the Store yet - you can obtain the value by calling the Windows.Security.Authentication.Web.WebAuthenticationBroker.GetCurrentApplicationCallbackUri() method. So for example, in the OnLaunched method of your application, you can add the following line of code:

// App.xaml.cs

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
#if DEBUG
    if (System.Diagnostics.Debugger.IsAttached)
    {
        System.Diagnostics.Debug.WriteLine(Windows.Security.Authentication.Web.WebAuthenticationBroker.GetCurrentApplicationCallbackUri());
    }
#endif

    // rest of code omitted for brevity
}

This will print out the callback URL to your Debug window in Visual Studio.

Once you have the correct callback URL, go to the Client Settings section in the Auth0 dashboard and make sure that Allowed Callback URLs contains the value of the callback URL, e.g.:

ms-app://S-1-xxx...

This is a bit of a painful process to obtain this URL, but it is important to use this URL otherwise the SSO will not function correctly.

Integration

To integrate Auth0 login into your application, simply instantiate an instance of the Auth0Client class, configuring the Auth0 Domain and Client ID:

// MainPage.xaml.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();

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.

// MainPage.xaml.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.

// MainPage.xaml.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:

// MainPage.xaml.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:

// MainPage.xaml.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