AUTHENTICATION AND AUTHORIZATION

STRUCTURE OF A RAZOR PAGE

NET CORE

A Razor Page offers an alternative way of conveying content to our users, In them we will find the same concepts that we encountered in MVC. We see the structural difference between MVC and Razor Page.

Struttura

For example, if we take the Registration Form we have a Register.cshtml View , and a Register.cshtml.cs file where Controller and Model are located. Let’s start by analyzing the View. Basically there are no major differences with an MVC View, the only thing to note is the @page directive at the top of the view.

file CSHTML

The @page directive makes the Razor Page reachable via the Web at a certain address. Let us analyze the directory hierarchy to understand this directive. Areas is a special ASP.NET Core container that does not affect the address. Identity which is the name of the Area affects the path, all contents within this directory are reachable by the /Identity prefix. Pages is also a special container of Razor Pages that does not affect the address. Account was defined by Microsoft. Finally, we have the Razor Page which is not to be taken in full but without the extension.

Page

Thanks to the @page directive, the registration page will be reachable by our users at /Identity/Account/Register. Let’s take an even simpler example.

Privacy

Razor pages are especially useful when we need to convey static text, where there is no logic behind it. Let’s see how the file defining the Controller and Model is organized.

MC

OnGetAsync is the method with a GET verb that is called to populate the View Razor; therefore, it can access a database, for example, and value the InputModel. OnPostAsync is the method with POST verb. As you can see there is a big similarity between the methods of a Controller in the MVC architecture and in Razor Pages.

Riepilogo

ADD A FIELD TO THE REGISTRATION RAZOR PAGE

Let’s see how to add a FullName field to the registration Razor page. We first modify the InputModel so that it accommodates the FullName property and then implement it in the Razor Page. So far there are no major difficulties also because we have seen about the Razor Pages that it will be the model binder that will enhance this new property. What we need to do is to change the default behavior of Identity and extend IdentityUser.

Estendere IdentityUser
Estendere Identity User

ApplicationUser It will inherit all the other properties, so we just need to add the FullName property. ApplicationUser will be persisted thanks to the UserManager and Entity Framework Core infrastructure services; therefore, it will have to be a class known to our DbContext. Let’s go inside MyCourseDbContext and have it inherit from IdentityDbContext<ApplicationUser>. We apply a Migrations, find it in the Migrations folder so that the AspNetUsers table includes the FullName field. Now we go back to the Razor Page and We complete the job by replacing IdentityUser with ApplicationUser.

ACCESS THE USER’S PROFILE AND UPDATE IT WITH THE USER MANAGER

Now our user’s e-mail appears as a welcome message. Now that we have the full name, let’s see how to make it appear to access the user profile. A user, for example, an author cannot be represented with an e-mail address. We need to go to the View _LoginPartial.cshtml And modify its behavior. Let us first see how to find the FullName.

UserManager
User Manager

Now that we know how to retrieve the FullName let’s look at the change to the view.

Partial view

Let’s see how to persist the user profile.

Persistere il profilo

LINK TO CODE ON GITHUB

GITHUB

Download the section17 code or the master branch or clone the GITHUB repository to have all the sections available in your favorite editor. See the README.md file to find user and password to send e-mail with mailtrap.io. You also need to find the codes to implement ReCaptcha. Below is an image from Google’s site.

Recaptcha