Tuesday, June 28, 2011

How to add a Captcha to your Login form Asp.Net in 10 foolproof steps

A quick and good solution to add Captcha’s to your solution is to use Google’s. There are various solutions available online to make your own captcha, but I prefer usingGoogle’s Recaptcha which can be found on:http://www.google.com/recaptcha

There is this free usercontrol available online.

Here are the 10 easy steps to follow:

1) sign up at https://www.google.com/recaptcha/admin/create

2) add a site

3) copy & paste your public and private key in notepad as reminder

4) download and extract the latest version of the .net control which can be found athttp://code.google.com/p/recaptcha/downloads/list?q=label:aspnetlib-Latest

5) put the extracted files in the Bin folder of your solution

6) The next thing is to add a reference in your project to the DLL in your Bin folder.

7) Register the control in the page (or usercontrol) by adding this to the top of the page:

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

8) Insert the control in your aspx (or ascx) file at the right place

1
2
3
4
5
6
<recaptcha:RecaptchaControl
ID="recaptcha"
runat="server"
PublicKey="your_public_key"
PrivateKey="your_private_key"
/>

9) Insert your public and private key which you stored with notepad.

10) Do not forget to validate the Recaptcha control. Here is how I did it in my code behind of an control:

1
2
3
4
5
6
protected void Login1_LoggedIn(object sender, EventArgs e)
{
//LogVisit();
recaptcha.Validate();
if (recaptcha.IsValid)
{ // etc