Click here to Skip to main content
15,885,216 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 144.4K   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

 
QuestionThe error I get Pin
Rachel825-Mar-21 11:19
Rachel825-Mar-21 11:19 
QuestionGood and simple - except Pin
Saavik1-Mar-19 18:04
Saavik1-Mar-19 18:04 
QuestionCan't get the control to center? Pin
Wizzert19-Feb-19 11:23
Wizzert19-Feb-19 11:23 
Questionalternative Pin
Member 1214658829-Nov-18 23:32
Member 1214658829-Nov-18 23:32 
QuestionI never get a checkmark Pin
Member 1374360324-Mar-18 21:18
Member 1374360324-Mar-18 21:18 
SuggestionLove it - but would be better with language option Pin
fpetya8616-Jan-18 5:29
fpetya8616-Jan-18 5:29 
GeneralRe: Love it - but would be better with language option Pin
Member 117817467-May-18 20:53
Member 117817467-May-18 20:53 
GeneralMy vote of 5 Pin
Charly-MX22-Oct-17 9:25
Charly-MX22-Oct-17 9:25 
PraiseGood Tutorial Pin
dineshkumarw21-Dec-16 22:37
professionaldineshkumarw21-Dec-16 22:37 
QuestionHow to set Public Key and Private key at runtime. Pin
Member 128892286-Dec-16 0:14
Member 128892286-Dec-16 0:14 
QuestionUsing two Google Recaptures on a single page Pin
Member 1286433022-Nov-16 2:05
Member 1286433022-Nov-16 2:05 
QuestionDoesnt Work Pin
TakoMT6-Sep-16 0:45
TakoMT6-Sep-16 0:45 
QuestionQuestion recaptcha v2 Pin
Member 1271419031-Aug-16 22:25
Member 1271419031-Aug-16 22:25 
PraiseAnother vote of 5 Pin
matthewproctor20-Jun-16 16:19
professionalmatthewproctor20-Jun-16 16:19 
QuestionHow to reference GoogleReCaptcha.dll in my project VisualStudio2008 Pin
silviapauit6-Jun-16 8:06
silviapauit6-Jun-16 8:06 
PraiseCouldn't be better Pin
rejyv16-Feb-16 10:58
rejyv16-Feb-16 10:58 
GeneralMy vote of 5 Pin
rejyv16-Feb-16 10:56
rejyv16-Feb-16 10:56 
GeneralMy vote of 5 Pin
Member 1031355110-Feb-16 2:10
Member 1031355110-Feb-16 2:10 
QuestionValidate always false Pin
Member 123064533-Feb-16 20:38
Member 123064533-Feb-16 20:38 
QuestionChange the display language of the reCaptcha 2.0 Pin
Alpine Owl7-Dec-15 5:55
Alpine Owl7-Dec-15 5:55 
Questionaudio/size/theme in captcha 2.0 Pin
Member 1213749413-Nov-15 7:27
Member 1213749413-Nov-15 7:27 
QuestionThanks for your help Pin
nguyenson0303.tk6se12-Nov-15 18:10
professionalnguyenson0303.tk6se12-Nov-15 18:10 
QuestionWell done, but ViewState is insecure Pin
esaulsberry22-Sep-15 9:56
esaulsberry22-Sep-15 9:56 
AnswerRe: Well done, but ViewState is insecure Pin
ALight224-Nov-15 12:12
ALight224-Nov-15 12:12 
QuestionNice example! Pin
Member 1198132613-Sep-15 17:24
Member 1198132613-Sep-15 17:24 

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.