Securing Razor Pages Applications with Auth0
Tags: dotnet, programming, tutorial
Published: 2022-06-06 21:20:53

### Let’s talk about how to add authentication to your ASP.NET Razor Pages application.

### <br>

Razor Pages is one of the programming models to create web applications in ASP.NET Core. Let's see how to add authentication support using the Auth0 ASP.NET Core Authentication SDK.

## Razor Pages vs. ASP.NET Core MVC

When it comes to building web applications with ASP.NET, you find yourself having to [choose between several programming models](https://dotnet.microsoft.com/en-us/apps/aspnet/web-apps). Putting aside Single Page Applications (SPA) and focusing only on the most recent versions of .NET, you have two programming models to create traditional web applications: ASP.NET Core MVC and Razor Pages. Which programming model should you choose for your web application?

The ASP.NET Core MVC model is more popular than the Razor Pages model, maybe because ASP.NET Core MVC has a longer tradition that started in 2009 with ASP.NET MVC. Actually, this doesn't mean that this programming model is better than the Razor Pages model.

Both programming models rely on the same template engine, Razor. However, ASP.NET Core MVC promotes the [Model-View-Controller (MVC) design pattern](https://docs.microsoft.com/en-us/aspnet/core/mvc/overview#mvc-pattern), while Razor Pages applications propose a lighter and more page-focused approach. So, when you have to choose what programming model to use for your web application, you should carefully evaluate where your application's behavior fits into. As [Microsoft documentation](https://docs.microsoft.com/en-us/dotnet/architecture/porting-existing-aspnet-apps/comparing-razor-pages-aspnet-mvc) states, "if your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages".

That said, if you have experience in using both programming models, you may note that the border between the two models is not so neat. Since both models share the same template engine, you may find ASP.NET Core MVC applications that use Razor Pages when a simple page is needed. On the other side, you may find Razor Pages applications that use controllers for functionalities where they make sense.

Mixing the two models lets you take the best of both to build efficient web applications.

To learn more about Razor Pages, check out the [official documentation](https://docs.microsoft.com/en-us/aspnet/core/razor-pages/). This article will focus on securing a Razor Pages application with Auth0. Check out [this article if you are looking to add authentication to an ASP.NET Core MVC application](https://auth0.com/blog/add-authentication-aspnet-core-mvc/).

## The Sample Application

This article will not drive you to build a Razor Pages application from scratch. Instead, you will modify an existing sample project built with C# 10. This means that you need the [.NET 6 SDK](https://dotnet.microsoft.com/download/dotnet/6.0) installed on your machine. To learn more about [the new features introduced by .NET 6, check out this article](https://auth0.com/blog/dotnet6-whats-new/).

While the instructions in this article will drive you to use the .NET CLI to build and run the application, you can use Visual Studio 2022 if you prefer.

### Get and run the sample application

You can download the sample application on your machine by running the following command in a terminal window:

```shell
git clone -b starter --single-branch https://github.com/auth0-blog/acme-aspnet-razor.git
```

Once you download it, move to the `acme-aspnet-razor` folder and type the following command to launch the application:

```shell
dotnet watch
```

This command will run the sample application and wait for possible changes to the code. If you change the application code, it will be automatically rebuilt.

> Note that some specific changes to your code, known as *rude edits*, may require restarting your application. Read [this](https://devblogs.microsoft.com/dotnet/introducing-net-hot-reload/#supported-unsupported-changes-and-languages) to learn more.

After a few seconds, your application is up and running. Point your browser to [`https://localhost:7204`](https://localhost:7204). You should see the following page:

![ACME website home page](https://images.ctfassets.net/23aumh6u8s0i/31xLAAHquxMY02w9rp4aPQ/23053c23185a88faaed1e20bf1b32ea9/acme-website-homepage-razor.png)

This is the home page of the fictional company [ACME Corporation](https://en.wikipedia.org/wiki/Acme_Corporation).

By clicking the *Catalog* link in the header, you can navigate their catalog, which will look as shown below:

![ACME catalog](https://images.ctfassets.net/23aumh6u8s0i/6X7vVwM2CPc7Lcdl6cWeZ8/171586fe76ce394dc5d60393e8df8e74/acme-catalog-razor.png)

Actually, the *Buy now!* buttons are not working. This page is just a placeholder for a page that users would expect to be protected. In other words, only authenticated users should access the catalog page. This is what you are going to implement in the next few sections.

[Read more...](https://auth0.com/blog/securing-razor-pages-applications-with-auth0/?utm_source=content_synd&utm_medium=sc&utm_campaign=auth0)