AUTHORIZATION

CREATE A CONTACT FORM WITH RAZOR PAGE

NET CORE

We will create a Contact Form where a student can send questions to the teacher. We will not store this information in the database, but we will send the application via e-mail reusing the MailKitEmailSender.cs infrastructure service. The Form will consist of a TextArea in which the student will type his question, and a button for sending the e-mail. It will be protected with Google reCAPTCHA Version two service so that the Form is protected from spammers. Let’s look at some of the implemented code.

Codice

SEND AN INFORMATION REQUEST E-MAIL

A student to request information from the teacher should fill out the contact form, and the application will automatically send an e-mail with the infrastructure service MailKitEmailSender.cs. I’ll show you the code. First of all, the method should be added

Task SendQuestionToCourseAuthorAsync(int id, string question);

At Interface ICourseService, this is the code implemented in AdoNetCourseService, analogous is the code for EfCoreCourseService.

AdoNetCourseService

AUTHORIZE HUMAN USERS WITH RECAPTCHA

There are tools that allow the sending of spam, one of which is Puppeteer. This application has been ported to Dotnet as well, and it allows one to write scripts interpreted by the Browser that automate for a certain number of times for example the sending of the contact form with targeted advertisements. To defend against bots, we need to make sure that the application is used only by human beings.

Bot

Our goal when using a CAPTCHA is to make things difficult for spammers.

captcha

WordPress also uses a CAPTCHA during registration, and this is a great idea to prevent information created by bots from ending up in the database.

Captcha

We will use Google’s CAPTCHA that only requires you to put a checkmark inside a field. Google evaluates mouse movements, and if it fails at this stage it may present other puzzles such as a series of images.

Captcha
Captcha

Let’s go and install reCAPTCHA, to do that let’s go to the address shown in the slide.

ReCaptcha

We choose version two with checkboxes, once the form is filled out you will have the two keys that we will need in the application.

Chiavi

Let’s see how version two of reCAPTCHA works.

Captcha Risolto

To use reCAPTCHA we download a package from NuGet, you can look it up in the MyCourse.Csproj file.

appsettings

We register the service in the Startup.cs file.

Startup.cs

We now insert the code into the cshtml file that consists of a single Tag Helper to be recorded in ViewImports.cshtml.

CSHTML

This is the attribute to place on the PageModel.

PageModel

LINK TO CODE ON GITHUB

GITHUB

Download the section18 code or the master branch or clone the GITHUB repository to have all the sections available in your favorite editor.