# Auth0 Authentication Example

## Prerequisites

In this example, you will need the following NuGet package

```
Auth0.AspNetCore.Authentication
```

## Setting up Ucommerce Backoffice

Setting up Ucommerce backoffice with Auth0 requires you to set up an external authentication scheme and [external claims mapper](/readme/backoffice-authentication.md#external-claims-mapper) when calling `.AddBackOffice()`

To set up the authentication, use a helper method from the Auth0 package to add all the required services via the [AuthenticationBuilder](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authentication.authenticationbuilder?view=aspnetcore-8.0). We highly recommend looking into some of these methods, as they do much of the groundwork to set up OpenID.

```csharp
var ucommerceBuilder = builder.Services
    .AddUcommerce(builder.Configuration)
    .AddBackOffice(securitySettings =>
        {
            securitySettings.AddExternalIdentityProvider<MyExternalClaimsMapper>(
                Auth0Constants.AuthenticationScheme,
                authenticationBuilder =>
                {
                    // Use the AuthenticationBuilder from ASP.NET to set up authentication
                    authenticationBuilder
                        .AddAuth0WebAppAuthentication(Auth0Constants.AuthenticationScheme,
                            auth0Options =>
                            {
                                auth0Options.Domain = builder.Configuration["Auth0:Domain"]!;
                                auth0Options.ClientId = builder.Configuration["Auth0:ClientId"]!;
                                // Ucommerce will handle the cookie session, so we disable it for Auth0
                                auth0Options.SkipCookieMiddleware = true;
                            });
                });
            // Configure Ucommerce to use your scheme from code
            securitySettings.UseExternalIdentityProvider(Auth0Constants.AuthenticationScheme);
        }
    )
    ...
```

{% hint style="info" %}
Remember to create your[ external claims mapper](/readme/backoffice-authentication.md#external-claims-mapper) to map the claims from Auth0 to Ucommerce.
{% endhint %}

## Create your Auth0 application

Create your application in the [Auth0 Dashboard](https://manage.auth0.com/dashboard).

When the application is created, update `appsettings.json` to keep track of `Domain` and `ClientId`.

```json
  "Auth0": {
    "Domain": "{YourDomain}",
    "ClientId": "{YourClientID}"
  },
```

For further guidance on setting up Auth0, we recommend the [Auth0 Quickstart Guide](https://auth0.com/docs/quickstart/webapp/aspnet-core/interactive).

## Related Articles

{% embed url="<https://auth0.com/docs/quickstart/webapp/aspnet-core/interactive>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dev.ucommerce.net/readme/backoffice-authentication/auth0-authentication-example.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
