Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Google ReCaptcha 2.0 - ASP.net Control

4.93/5 (36 votes)
9 Mar 2015CPOL1 min read 163.6K   14.7K  
.Net control/wrapper for the Google NEW ReCaptcha API 2.0

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:

C++
<%@ Register Assembly="GoogleReCaptcha" Namespace="GoogleReCaptcha" TagPrefix="cc1" %>

Then, you can use the control wherever you want to use this control like given below:

C++
<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:

C++
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:

C++
if (ctrlGoogleReCaptcha.Validate())
{
   //submit form success
   lblStatus.Text = "Success";
}
else
{
    //captcha challenge failed
    lblStatus.Text = "Captcha Failed!! Please try again!!";
}

History

Keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)