DEV Community

Cover image for Auth0 Templates for .NET
Andrea Chiarelli for Okta

Posted on • Edited on

Auth0 Templates for .NET

If you are a .NET developer and have been using Auth0 by Okta's authentication and authorization services, chances are that you've come across one of my articles. 😎

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 and OAuth2 is not easy. Fortunately, SDKs provide a great deal of help in abstracting the complexity inherent in the protocols. But even knowing which SDK to use 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, 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. You can even not provide the configuration parameters if you have the Auth0 CLI installed on your machine. But let's go one step at a time.

First, you need to install the NuGet package. You can do this by running the following command in a terminal window:

dotnet new install Auth0.Templates
Enter fullscreen mode Exit fullscreen mode

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

  • Auth0 MVC Application
  • Auth0 Web API Application
  • Auth0 Blazor Web App
  • Auth0 Blazor Server Application
  • Auth0 Blazor WebAssembly Application
  • Auth0 MAUI Application

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

Create your Auth0-enabled .NET Project with the .NET CLI

You can use these templates from the command line, through the .NET CLI, 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. During this first step, you will get some settings that you will use to set up your app through the templates.

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

dotnet new auth0blazorserver --domain <YOUR_AUTH0_DOMAIN> --client-id <YOUR_CLIENT_ID> -o MyBlazorApp
Enter fullscreen mode Exit fullscreen mode

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

Add the power of the Auth0 CLI

If you have the Auth0 CLI installed on your machine and logged in to your Auth0 tenant, you can even skip the registration step. You can simply run the following command:

dotnet new auth0blazorserver -o MyBlazorApp
Enter fullscreen mode Exit fullscreen mode

The Auth0 CLI will automatically register you application with Auth0 and configure it on your behalf. You can have a working .NET application in less than a minute! :🧞‍♂️

Create your Auth0-enabled .NET Project with Visual Studio

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

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

Auth0 settings in Visual Studio for Windows

Unfortunately, due to a Visual Studio limitation in running external script, you can't leverage the Auth0 CLI magic to automatically register your application in this case.

You can also use the templates in Visual Studio for Mac and JetBrains Rider.

Stay Tuned!

I'd like the Auth0 Templates for .NET project 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 and let me know what do you think. Your feedback is welcome, as well as your contribution. 🙏

Top comments (0)