Monday, June 22, 2009

Security using CAPTCHA Library

Recaptcha asp.net quickstart

These instructions should get you started quickly.

  1. Download the reCAPTCHA Library.
  2. Add a reference to library/bin/Release/Recaptcha.dll to your website
  3. Insert the reCAPTCHA control into the form you wish to protect. At the top of the aspx page, insert the following code:

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

Then insert the reCAPTCHA control inside of the <form runat="server"> tag:

<recaptcha:RecaptchaControl

ID="recaptcha"

runat="server"

PublicKey=""

PrivateKey=""

>

  1. If you haven't done so, sign up for an API key. Put the public and private key in the PublicKey and PrivateKey properties
  2. Make sure you use ASP.NET validation to validate your form (you should check Page.IsValid on submission)

Example

The following is a "Hello World" with reCAPTCHA using C# :

<%@ Page Language="cs" %>

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

<script runat=server>

Protected void btnSubmit_Click(object sender, EventArgs e)

If( Page.IsValid)

{

lblResult.Text = "You Got It!";

lblResult.ForeColor = System.Drawing.Color.Green;

}

else

{

lblResult.Text = "Incorrect";

lblResult.ForeColor = System.Drawing.Color.Red;

}

</script>

<html>

<body>

<form runat="server">

<asp:Label Visible=false ID="lblResult" runat="server" />

<recaptcha:RecaptchaControl

ID="recaptcha"

runat="server"

PublicKey=""

PrivateKey=""

/>

<br />

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

</form>

</body>

</html>

Popular Posts