Click here to Skip to main content
15,867,830 members
Articles / Web Development / ASP.NET

Recaptcha

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
22 Oct 2013CPOL2 min read 29.5K   12   2
Recaptcha:Captcha keeps the spammer away from our site. There is Lot of captcha conrols available for asp.net and few may not work on postback.

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Recaptcha:

Captcha keeps the spammer away from our site. There is Lot of captcha conrols available for asp.net and few may not work on postback. Google has released a captcha control as “recaptcha” for asp.net developer and its easy plug and more secure and customizable and its Totally free all you need is gmail id.


To Use “recaptcha” Control on your website you must register your site and get an API Key.


Visit the link https://www.google.com/recaptcha/admin/create


After registering your site Google will provide the Two Keys
•    Public Key and
•    Private Key 


To use recaptcha with ASP.NET, you MUST download the DLL file


http://code.google.com/p/recaptcha/downloads/detail?name=recaptcha-dotnet-1.0.5.0-binary.zip&can=2&q=label%3Aaspnetlib-Latest


Now Download folder contains a file Recaptcha.dll add the file to bin folder of your project and reference dll to your project.
Now create a blank aspx page and add header to the page

ASP
<% @ Register TagPrefix ="recaptcha" Namespace ="Recaptcha" Assembly ="Recaptcha" %>

<recaptcha:RecaptchaControl ID="recap" PublicKey="Yourpublic Key" PrivateKey="Your Private Key" runat="server" />

To validate this recaptcha add few below line of code in button click event 

C#
if (Page.IsValid == true )
{
    Label1.Text = "Capacta Right";
}
else
{
    Label1.Text = "Wrong";
}

Customize:
           You can set Theme for  recaptcha control.By Default red theme is set.
There are Four themes


ASP
<recaptcha:RecaptchaControl ID="recap" PublicKey="Yourpublic Key" PrivateKey="Your Private Key" runat="server" Theme="clean" /> 

 Clean theme look more professional than other themes.


Custom Themes and Size:
             We can Create our custom theme and resize the recaptcha. To do First set theme property as custom.

ASP
<recaptcha:RecaptchaControl ID="recap" PublicKey="Yourpublic Key" PrivateKey="Your Private Key" runat="server" Theme="custom" CustomThemeWidget="recaptcha_widget"   /> 

Captcha has Image,textbox,Refresh button,Audio Captcha and Help Buttons and we can remove any these button as per our need to do so paste  the script in anywhere in design source.

ASP
 <div id="recaptcha_widget" style=" display : none">
 < div id ="recaptcha_image"></div>
 <div class="recaptcha_only_if_incorrect_sol" style=" color : red"> Incorrectplease try again </div>
 <span class="recaptcha_only_if_image"> Enter thewords above: </span>
 <span class="recaptcha_only_if_audio"> Enter thenumbers you hear: </span>
 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
 <div><a href="javascript:Recaptcha.reload()"> Getanother CAPTCHA </a></div>
 <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')"> Getan audio CAPTCHA </a></div >

 <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')"> Getan image CAPTCHA </ a ></ div >
 <div><a href="javascript:Recaptcha.showhelp()"> Help </a></div>
</div>
<script type ="text/javascript"
  src="http://www.google.com/recaptcha/api/challenge?k=your_public_key">
</script >
<noscript>
 <iframe src="http://www.google.com/recaptcha/api/noscript?k=your_public_key"
      height="300" width="500" frameborder="0"></iframe><br>
 <textarea name="recaptcha_challenge_field" rows="3" cols="40">
 </textarea>
 <input type="hidden" name ="recaptcha_response_field"
      value="manual_challenge">
</noscript>

 Two thing need to be noticed while you paste the above code


1. Div Tag must have same ID as CustomThemeWidget="recaptcha_widget"
2. Replace your public key in the above snippets.(Inside the Script Tag)

Now Custom captcha look like below.

 

 

 

We can remove the “Get another captcha” or “Get an Audio captcha” or “help”.

Now we try to remove the Get audio captcha and Help Link.
To do so, Just remove the below two lines from above snippets
 

ASP
<div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')"> Getan audio CAPTCHA </a></div >
<div><a href="javascript:Recaptcha.showhelp()"> Help </a></div>

 To change background color define background color   in css class for ID “recaptcha_widget”

ASP.NET
<div id="recaptcha_widget" style="background-color:#f7f7f7;"> 
 To change the width:
ASP.NET
<div id="recaptcha_widget" style="background-color:#f7f7f7;width:300px;">

and width should be minimum 300px.

Sites that use recaptcha is facebook,wikipedia,tinypic...

 

 

 

 

 

This article was originally posted at http://wiki.asp.net/page.aspx/1567/recaptcha

License

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


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
QuestionRecaptcha doesn't seem to work on localhost Pin
Davem771419-Jul-17 3:25
Davem771419-Jul-17 3:25 
QuestionCustomize code length in google recaptcha Pin
deepearth25-May-15 0:34
deepearth25-May-15 0:34 

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.