How to Use Recaptcha in an ASP.NET Web Forms Application

Pre-requisites

Please ensure that you have completed the following steps:

Add the Recaptcha Control to Your Page or User Control

Add the following line just under the Page directive in your .aspx or .ascx file:

<%@ Register Assembly="Recaptcha.Web" Namespace="Recaptcha.Web.UI.Controls" TagPrefix="cc1" %>

Then at the desired line in the same file add the Recaptcha control as follows:

<cc1:Recaptcha ID="Recaptcha1" PublicKey="6LdkfdwSAAAAABL1099CPTr6473FQFXNLR_04Bb5" PrivateKey="6LdkfdwSAAAAAFC8jtUY44wuhC9lmDlFrL6qMAAh" runat="server" />

Rather than setting the recaptcha key of the control through its PublicKey and PrivateKey properties, you can set them in your web.config file instead: After you set the private and public keys in your web.config file, all you need in your web form is this following piece of code:

<cc1:Recaptcha ID="Recaptcha1" runat="server" />

By default, the theme of the Recaptcha control is Red. However, you can change this default theme to one of the other three themes if you like. Those themes are: Blackglass, White, and Clean. Theme can be set by using the RecaptchaTheme enum. The following is an example:

<cc1:Recaptcha ID="Recaptcha1" Theme="RecaptchaTheme.Clean" runat="server" />

Add the Recaptcha Control to the Visual Studio Toolbox

Instead of writing the above code manually, you can easily drag and drop the same Recaptcha control from the Visual Studio Toolbox onto your page designer just like the way you would do for other standard ASP.NET controls. However, you would need to add the Recaptcha control to the Toolbox first. Simply, right click on the Toolbox and select Choose Items... from the context menu and then under the .NET Framework Components tab click on the Browse button and locate the Recaptcha.Web.dll assembly.

Verify User's Response to Recaptcha Challenge

When your end-user submits the form that contains the Recaptcha control, you obviously would want to verify whether the user's answer was valid based on what was displayed in the recaptcha image. It is very easy to do with one or two lines.

First of all as expected, import the namespace Recaptcha.Web in your code-behind file:

using Recaptcha.Web;

To verify whether the user's answer is correct, call the control's Verify() method which returns RecaptchaVerificationResult. You can also use the control's Response property to check what the actual answer is. Generally, you would want to use the Response property to check if the user provided a blank response which of course is always wrong:

if (String.IsNullOrEmpty(Recaptcha1.Response))
{
    lblMessage.Text = "Captcha cannot be empty.";
}
else
{
    RecaptchaVerificationResult result = await Recaptcha1.Verify();

    if (result == RecaptchaVerificationResult.Success)
    {
        Response.Redirect("Welcome.aspx");
    }
    if (result == RecaptchaVerificationResult.IncorrectCaptchaSolution)
    {
        lblMessage.Text = "Incorrect captcha response.";
    }
    else
    {
        lblMessage.Text = "Some other problem with captcha.";
    }
}

Instead of calling the Verify() method, you can call the VerifyTaskAsync() method to verify the user's response asynchronously which at the same time can be used along with the new await keyword:

if (String.IsNullOrEmpty(Recaptcha1.Response))
{
    lblMessage.Text = "Captcha cannot be empty.";
}
else
{
    RecaptchaVerificationResult result = await Recaptcha1.VerifyTaskAsync();

    if (result == RecaptchaVerificationResult.Success)
    {
        Response.Redirect("Welcome.aspx");
    }
    if (result == RecaptchaVerificationResult.IncorrectCaptchaSolution)
    {
        lblMessage.Text = "Incorrect captcha response.";
    }
    else
    {
        lblMessage.Text = "Some other problem with captcha.";
    }
}

See Also

Last edited Feb 27, 2013 at 7:12 AM by tanveery, version 40

Comments

DRiley5998 Mar 6, 2015 at 9:26 AM 
I am using Visual Studio 2013 the above code is incorrect the await operator should be removed in the first example, as the return type is not of type task.

As far the second example gives an error -
The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

Kibbs Apr 28, 2013 at 12:25 PM 
Hello, I am using Visual Studio 2012, I'm new at this, how do I "Call" the verify method in the code behind when using the application template that includes account/registration.aspx??? This was roughly one of the best tutorials for using the recaptcha method with asp.net I've found online. Minor exception, for a newbie like me, I just dont know how to call this in the code behind.. Please advise, Thanks!