Click here to Skip to main content
15,893,588 members
Articles / Web Development / ASP.NET
Tip/Trick

Google ReCaptcha 2.0 - ASP.net Control

Rate me:
Please Sign up or sign in to vote.
4.93/5 (36 votes)
8 Mar 2015CPOL1 min read 145.2K   14.6K   37   39
.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)


Written By
Team Leader 10Pearls
United Arab Emirates United Arab Emirates
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNice example! Pin
Member 1198132613-Sep-15 17:24
Member 1198132613-Sep-15 17:24 
GeneralMy vote of 5 Pin
Jon A. Casper30-Aug-15 9:42
Jon A. Casper30-Aug-15 9:42 
QuestionAsp.NET 3.5 compatibility Pin
enrico_c14-Jul-15 20:59
enrico_c14-Jul-15 20:59 
AnswerRe: Asp.NET 3.5 compatibility Pin
Alexander Melo23-Jul-15 5:39
Alexander Melo23-Jul-15 5:39 
QuestionPerfect Pin
A_ndre9-Jul-15 8:40
A_ndre9-Jul-15 8:40 
QuestionIs there a way to get it to work inside UpdatePanel? Pin
DancnDude7-Jul-15 5:57
DancnDude7-Jul-15 5:57 
BugCannot be used on GoDaddy Pin
RLebeau1-Jul-15 16:59
RLebeau1-Jul-15 16:59 
QuestionCan you hide the "Privacy - Terms" label from the CAPTCHA control Pin
Astak1-Jun-15 13:25
Astak1-Jun-15 13:25 
Komail - Thank you for the great solution.

Is there a way we can hide the "Privacy - Terms" label from this CAPTCHA control?
I see google is serving the control in an iframe. For that reason client side CSS such as

.rc-anchor-pt {display: none; or visibility: hidden:}

is unable to hide this label. Besides the iframe name or id changes every time the control is loaded making it difficult to using JavaScript to hide the .rc-anchor-pt class programmatically. Is there a way you can do this inside your GoogleReCaptcha.cs file?

Thanks
AnswerRe: Can you hide the "Privacy - Terms" label from the CAPTCHA control Pin
Komail Haider15-Jun-15 8:25
Komail Haider15-Jun-15 8:25 
GeneralMy vote of 5 Pin
Steve Eagle2-Apr-15 10:13
Steve Eagle2-Apr-15 10:13 
GeneralRe: My vote of 5 Pin
Komail Haider2-Apr-15 10:39
Komail Haider2-Apr-15 10:39 
GeneralMy vote of 5 Pin
newton.saber9-Mar-15 1:56
newton.saber9-Mar-15 1:56 
GeneralRe: My vote of 5 Pin
Komail Haider9-Mar-15 2:47
Komail Haider9-Mar-15 2:47 
QuestionNice Effort :) Pin
M I developer9-Mar-15 1:13
professionalM I developer9-Mar-15 1:13 
AnswerRe: Nice Effort :) Pin
Komail Haider9-Mar-15 2:47
Komail Haider9-Mar-15 2:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.