Auth0 Templates for .NET
Tags: auth0, dotnet, template, authentication
Published: 2023-09-12 08:47:46

If you are a .NET developer and have been using [Auth0 by Okta](https://auth0.com/)'s authentication and authorization services, chances are that you've come across [one of my articles](https://auth0.com/blog/authors/andrea-chiarelli/). 😎

Over the past few years, I have tried to explain in simple and, most importantly, practical terms how to integrate Auth0 into the various types of applications you can build with .NET. My goal has been to simplify the experience of .NET developers approaching Auth0.

The topic of authentication and authorization is complex, and untangling the details of protocols like [OIDC](https://openid.net/developers/how-connect-works/) and [OAuth2](https://oauth.net/2/) is not easy. Fortunately, [SDKs](https://auth0.com/docs/libraries) provide a great deal of help in abstracting the complexity inherent in the protocols. But even [knowing which SDK to use](https://auth0.com/blog/choose-the-right-dotnet-sdk-for-auth0/) based on the type of application and how to write the code to integrate Auth0 when an SDK is not available requires some familiarity with both the Auth0 product and the infrastructure provided by .NET.

## Here are the Auth0 Templates for .NET!

To simplify the development experience for .NET developers, I tried to go beyond creating tutorials and started the [Auth0 Templates for .NET project](https://a0.to/dev-to/auth0-templates-prj), a package of .NET project templates with everything needed to support Auth0's authentication and authorization services! 🎉

For your next .NET application using Auth0, you only need to select the right template and provide the configuration parameters from the Auth0 dashboard. But let's go one step at a time.

First, you need to install [the NuGet package](https://a0.to/dev-to/auth0-templates-pkg). You can do this by running the following command in a terminal window:

```bash
dotnet new install Auth0.Templates
```

Once the package is installed, you will have four templates to create your own applications:

- Auth0 MVC Application
- Auth0 Web API Application
- Auth0 Blazor Server Application
- Auth0 Blazor WebAssembly Application

The project is a work in progress, and more templates will be added in future releases.

## Create your Auth0-enabled .NET Project

You can use these templates from the command line, through the [.NET CLI](https://learn.microsoft.com/en-us/dotnet/core/tools/), or in Visual Studio.

Say you want to create a new Blazor Server application that uses Auth0 as its Identity provider. The first step is to [register your app with Auth0](https://auth0.com/docs/get-started/auth0-overview/create-applications). During this first step, you will get some data that you will use to set up your app through the templates.

Then, on your machine, run the following command in a terminal window:

```bash
dotnet new auth0blazorserver --domain <YOUR_AUTH0_DOMAIN> --client-id <YOUR_CLIENT_ID> -o MyBlazorApp
```

You specify the `auth0blazorserver` template as the template to use. Then, you pass your Auth0 domain and your client ID, values that come from the registration step. Finally, you indicate the folder where the new application will be created.

Voila! After a few seconds, you will find a new Blazor Server project in the folder you specified with all the code to support authentication via Auth0! 🪄

If you prefer to use Visual Studio, you just need to select the Auth0 template from the list, as shown below:

![Select Auth0 template from Visual Studio for Windows](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbj1tuxaljahudn86c2k.png)

Then, provide the required settings as shown in the following picture:

![Auth0 settings in Visual Studio for Windows](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqbo9j4iqg7cqmje6si9.png)

By the way, you can also use the templates in Visual Studio for Mac.

## Stay Tuned!

The [Auth0 Templates for .NET project](https://a0.to/dev-to/auth0-templates-prj) started as an Open Source project a few months ago. I'd like it to become the starting point for building any .NET application with Auth0 embedded. I plan to add more templates and improve the developer experience in the near future.

[Give it a try](https://a0.to/dev-to/auth0-templates-pkg) and let me know what do you think. Your feedback is welcome, as well as your contribution. 🙏