Introduction
This article provides a solution to integrate NEW Google Captcha control easily into asp.net application. Here is the screen cap of new ReCaptcha:
Background
The solution is based on the Google API docs i.e. https://developers.google.com/recaptcha/docs/display. It uses the automatic way of rendering Google reCaptcha on the ASP.net page.
Using the code
Download the attached DLL file i.e. "GoogleReCaptcha.dll" and reference it to any ASP.net project. Then, add reference to this ReCaptcha control on ASP.net page where you want to display Captcha control.
For example:
<%@ Register Assembly="GoogleReCaptcha" Namespace="GoogleReCaptcha" TagPrefix="cc1" %>
Then, you can use the control wherever you want to use this control like given below:
<cc1:GoogleReCaptcha ID="ctrlGoogleReCaptcha" runat="server" PublicKey="YOUR_SITE_KEY" PrivateKey="YOUR_SECRET_KEY" />
The public and private keys for captcha can be taken from Google i.e. https://www.google.com/recaptcha/. The GoogleReCaptcha control can also be added dynamically onto the ASP.net page using code below:
GoogleReCaptcha.GoogleReCaptcha ctrlGoogleReCaptcha = new GoogleReCaptcha.GoogleReCaptcha();
protected override void CreateChildControls()
{
base.CreateChildControls();
ctrlGoogleReCaptcha.PublicKey = "YOUR_SITE_KEY";
ctrlGoogleReCaptcha.PrivateKey = "YOUR_SECRET_KEY";
this.Panel1.Controls.Add(ctrlGoogleReCaptcha);
}
After adding the GoogleReCaptcha control, using any of the two above ways the NEW Google ReCaptcha control gets rendered on the ASP.net page. Now, the next requirement is to validate the Captcha Challenge on form submission.
Validate Captcha Challenge
In order to validate captcha challenge, you just need to call "Validate" method of the GoogleReCaptcha control. The Validate method returns boolean True/False. The sample code is given below:
if (ctrlGoogleReCaptcha.Validate())
{
lblStatus.Text = "Success";
}
else
{
lblStatus.Text = "Captcha Failed!! Please try again!!";
}
History
Keep a running update of any changes or improvements you've made here.