How to implement Google reCAPTCHA in ASP.NET MVC
By FoxLearn 2/18/2024 1:08:25 AM 263
To integrate Google reCAPTCHA V2 in your websites, you need to go to Google reCaptcha website, then login with your google account.
Next, Choose the reCAPTCHA v2 option, then add your domain in the list of domains
Click Register button, You will get a site key and a secret key.
Open your Manage Nuget Packages, then install ReCaptcha-AspNet or you can install from Nuget command line
Install-Package ReCaptcha-AspNet
Open your web.config under the appSettings section, add your secret and public keys as the following.
<add key="recaptcha-public-key" value="Your public key" /> <add key="recaptcha-private-key" value="Your private key" />
Open your html layout, then modify your code as shown below
@using(Html.BeginForm("Home","Test"){ @ReCaptcha.GetCaptcha() <input type="submit"> }
Create a test action in your home controller
[HttpPost] public ActionResult Test() { string captchaResponse = Request.Form["g-recaptcha-response"]; if (ReCaptcha.ValidateCaptcha(captchaResponse)) { return View(); } // A Bot return RedirectToAction("Robot"); }
and don't forget to include the following namespace to your class
using hbehr.recaptcha;
- How to use CORS in ASP.NET Core
- How to Send Emails in ASP.NET Core
- How to Run Background Tasks in ASP.NET Core with Hosted Services
- Implementing Scheduled Background Tasks in ASP.NET Core with IHostedService
- Creating an Web API in ASP.NET Core
- 8 Essential Tips to Protect Your ASP.NET Core Application from Cyber Threats
- 10 Common Mistakes ASP.NET Developers Should Avoid
- How to Upload Files Using C# ASP.NET FileUpload Control