In this tutorial, we will learn how to implement new Google reCaptcha in ASP.NET MVC Application using Google reCaptcha API. Google introduced a new API reCaptcha. This is very helpful and useful for human, but not easy for robot. With just a single click, it will confirm that they are not a robot. Google reCaptcha is a free service that helps to protect to our sites from spam.
This is very easy to implement in a project. So now we are going to understand how to use Google reCaptcha in ASP.NET MVC Application.
ASP.NET MVC Google Recaptcha
Follow the following steps how to implement Google reCaptcha in a project using Google reCaptcha API.
Step # 1 – Create a new project
Go to file => New => Project => ASP.NET Web Application from middle pane => Enter the name (E.g. GoogleReCaptchaDemo) in the name field => then click ok button. It will open a new dialog.
Step # 2 – Add reference of Newtonsoft.Json from NuGet Packages.
Select solution explorer from right pane => Right click on Reference => Manage NuGet Packages => Search for “Newtonsoft.Json” => click install.
Now add “System.Linq.Dynamic” in your project.
Search again for “System.Linq.Dynamic” in NuGet Packages search box => select and click install.
Step # 3 – Sign-up and Generate reCaptcha API
Go to this link https://www.google.com/recaptcha/intro/index.html. Then click on the top right corner Get reCaptcha button. Then a new page will open and you will also get a form for register a new site. Now fill the form and then complete your registration. After completing form, you will get a Site key and Secret Key. We will use these keys in our application.
Note:- if you want to get Google reCaptcha API for localhost then you must enter “localhost” in the Domains text area otherwise your domain name.
Step # 4 – Create a Controller.
Now go to solution explorer => Right click on the controller folder => choose Add => Controller => select “MVC 5 Controller – Empty” => click Add button, it will open a new dialog.
Now enter the name (E.g. HomeController) in the name field => then click Add button.
It will create a new HomeController.cs class under the controller folder in the solution explorer.
Step # 5 – Add New Action Method.
Enter the following action method to the HomeController.cs class
Step # 6 – Add the View for Index method
Go to Index action method => Just right-click on the Index action method => Add View. It will create Add View dialog.
Enter the name of View (E.g. Index) => Select Empty (without model) template under the template dropdown => then Click Add button.
Now Add the following HTML Code in the index view.
Step # 7 – Add an another Action Method.
Now we will add the new action method for form submit. Add the following code to your HomeController.cs class.
Now add the following two References in your HomeController.
Now run your application with this URL “ http://localhost:54832/Home/index ”, you will get the following output in your browser page.
I hope it will be helpful for you. please like and share !