Friday, September 23, 2011

reCAPTCHA ASP.NET library

I recently had a requirement to implement a CAPTCHA for one of our contact forms to minimize spam.

After much research and consideration I decided to use reCAPTCHA and the reCAPTCHA ASP.NET library.

Step 1 - Create a free account
http://www.google.com/recaptcha

Step 2 - Download the reCAPTCHA ASP.NET library
http://code.google.com/apis/recaptcha/docs/aspnet.html

I'm using the .Net control but as you'll see it has plugins for several platforms.

To get it working was just a matter of referencing the library in my project, dropping the control on my page and referencing the public/private keys generated in step 1.

12 comments:

  1. I can't find the aspnet library anywhere! It is not available in the google downloads!?! Any ideas / help?!?!

    ReplyDelete
  2. Looks like their link is not quite right, so I had to take out the search criteria and then it worked fine. Try this direct link:

    http://code.google.com/p/recaptcha/downloads/list?can=2&q=&colspec=Filename+Summary+Uploaded+ReleaseDate+Size+DownloadCount

    ReplyDelete
  3. yeah, but which one do I need for aspx ?

    ReplyDelete
  4. I am using the current release, which is "reCAPTCHA .NET Library v1.0.5.0 Binary Only"

    ReplyDelete
  5. ok..you're going to be sorry now you replied back so quickly!! :)


    Can you give me more precise instructions on how to actually get the thing to work and add the catpcha quetion to my form!?!
    I'm a real novice here and this has being driving me crazy all day!!

    ReplyDelete
  6. The first thing you need to do is add a reference in your web project to the Recaptcha.dll that you extracted from the download.

    Then you will want to register the assembly on the .aspx page.

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

    Next place the control on your page wherever you want it to show up.

    < recaptcha:RecaptchaControl ID="recaptcha" PublicKey="YourPublicKey" PrivateKey="YourPrivateKey" Theme="white" runat="server" />

    If you don't have a public/private key yet you can get one from http://www.google.com/recaptcha

    Then use a button (or whichever control you want) to initiate your postback.

    < asp:Button id="btnSubmit" Text="Submit" runat="server" />

    Then have some kind of postback event in your code-behind to validate the postback.

    Private Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
    'validate captcha
    If Not Page.IsValid Then
    'recaptcha phrase entered was not correct
    Return
    End If

    'validation successful
    End Sub

    Hope that helps, good luck!

    ReplyDelete
  7. Ok, I got most of that, but how do I reference the .dll from the .aspx file?

    ReplyDelete
  8. If you are using Visual Studio you can right click on your web project in the Solution Explorer and choose "Add Reference..." and then browse to the Recaptcha.dll.

    Then all you need to do is register the control on your .aspx page.

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

    ReplyDelete
  9. I'm building the site using DreamWeaver...if that means anything?

    ReplyDelete
  10. You can probably just copy the Recaptcha.dll into your /bin folder then.

    ReplyDelete
  11. ok, thanks Matt...much appreciated!!

    ReplyDelete