Skip to content

Instantly share code, notes, and snippets.

@rahuldass
Last active August 29, 2015 14:01
Show Gist options
  • Save rahuldass/5312f88d2c3914ea9c48 to your computer and use it in GitHub Desktop.
Save rahuldass/5312f88d2c3914ea9c48 to your computer and use it in GitHub Desktop.
Using Google reCAPTCHA with ASP.NET #asp.net #captcha

Google reCAPTCHA with ASP.NET


The reCAPTCHA ASP.NET Library provides a simple way to place a CAPTCHA on ASP.NET website, help us stop bots from abusing it.

Download the reCAPTCHA Library here

Steps

After signed up for your API keys, below are basic instructions for installing reCAPTCHA on site with ASP.NET:

Add a reference: On the Visual Studio Website menu, choose Add Reference and then click the .NET tab in the dialog box. Select the Recaptcha.dll component from the list of .NET components and then click OK. If you don't see the component, click the Browse tab and look for the assembly file on your hard drive.

Insert the reCAPTCHA control into the form you wish to protect by adding the following code snippets:

ASP.NET Code

At the top of the .aspx page, insert this:

<%@ 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="your_public_key"
    PrivateKey="your_private_key"
/>

You will need to substitute your public and private key into PublicKey and PrivateKey respectively.

C# Code

On Submit button click event

protected void btnSubmit_Click(object sender, EventArgs e)
{
    lblResult.Visible = true;

    if (recaptcha.IsValid)
    {
        lblResult.ForeColor = System.Drawing.Color.Green;
        lblResult.Text = "Correct!!!";
    }
    else
    {
        lblResult.ForeColor = System.Drawing.Color.Red;
        lblResult.Text = "Incorrect!!!";
    }
}

For Customization visit this link here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment