# 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](https://dev.ucommerce.net/readme/backoffice-authentication/..#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](https://dev.ucommerce.net/readme/backoffice-authentication/..#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>" %}
