Sample: Create a custom CAPTCHA forms widget
Overview
The following article describes how to create a custom CAPTCHA widget using Google reCAPTCHA v2 or v3. This is a forms widget and you can place it in a form.
For more information, see Create and design forms.
PREREQUISITES: Before using Google reCAPTCHA, you must:
- Register your site get an API key pair at Google reCAPTCHA.
You can generate an API key for either reCAPTCHA v2 or reCAPTCHA v3 and then use it with the corresponding widget from this tutorial.
- You must update your privacy and GDPR statements according to Google reCAPTCHA policies.
Configure Sitefinity CMS
Before implementing the widget, you must configure Sitefinity CMS.
Perform the following:
- In Sitefinity backend, navigate to Administration » Settings » Advanced.
- In the treeview, expand WebSecurity » Captcha and click Parameters.
- Create the following parameters:
- Save your changes.
Create the ViewComponent
You create the widget’s ViewComponent
in the Renderer application.
Perform the following:
- Open the ASP.NET Core Renderer in Visual Studio.
- In the context menu of the Renderer project, click Add » New Folder.
- Name the folder ViewComponents
- Inside the folder, add a class and name it Captcha2ViewComponent.cs or Captcha3ViewComponent.cs, depending on the version of the Google reCAPTCHA you want to create.
- In the class paste one of the following codes:
reCAPTCHA v2
reCAPTCHA v3
Create the View
You create the widget’s View
in the Renderer application.
Perform the following:
- In the context menu of the Renderer project, click Add » New Folder and create one of the following folder structures, depending on the version of the Google reCAPTCHA that you want to create:
Views/Shared/Components/Captcha2
Views/Shared/Components/Captcha3
- Inside the
Captcha2
or Captcha3
folder, add a code file and name it Default.cshtml
- In the file paste one of the following codes:
reCAPTCHA v2
reCAPTCHA v3
Create the Script
You create the widget’s script in the Renderer application.
Perform the following:
- In the context menu of the Renderer project, click Add » New Folder.
- Create a folder and name it Scripts
- Inside the
Scripts
folder, add a code file and name it captcha2.js or captcha3.js, depending on the version of the Google reCAPTCHA that you want to create.
- In the file paste one of the following codes:
reCAPTCHA v2
reCAPTCHA v3
NOTE: In the code samples above, change the <Captcha V2 site key>
or <Captcha V3 site key>
with the actual site key from the configured API key pair.
RESULT: After you build the project, the widget is displayed inside the forms widget selector when you are editing or creating ASP.NET Core forms.
Run the sample
This sample is available in Sitefinity’s GitHub repository. You can run and play with it.
To do this, perform the following:
- Go to Sitefinity’s GitHub repository Sitefinity ASP.NET Core samples.
- Expand Code and click Download ZIP.
- Extract the files on your computer.
- In the extracted folder, navigate to
sitefinity-aspnetcore-mvc-samples-master/src/captcha
folder.
- Open the
captcha.sln
in Visual Studio.
- Open the
appsettings.json
file.
- In section
“Sitefinity”
, change the “Url”
property to the URL of your Sitefinity CMS site.
If you have deployed Sitefinity CMS on the IIS, point to “https://localhost:<https_port>".
- In Visual Studio, in the context menu of
captcha
project, click View in Browser.
- Log in to your Sitefinity CMS instance and place the widget on a page.