ASP.NET CAPTCHA Validator C# Code Sample
First Time Here?
Check the BotDetect Developer Crash Course for key integration steps.
The ASP.NET Captcha Validator sample project shows how to use the CaptchaValidator control to integrate BotDetect CAPTCHA validation with standard ASP.NET page validation functionality and other validator controls.
- C#
- VB.NET
Visual Studio 2010 / .NET 4.0
By default, the .NET 4.0 C# version of the ASP.NET Captcha Validator sample project is installed at:
C:\Program Files\Lanapsoft\BotDetect 3 CAPTCHA Component\Asp.Net\v4.0\WebApp\AspNetValidatorCaptchaSample\CSharp
You can also run it from the BotDetect Start Menu:
Programs > Lanapsoft > BotDetect 3 CAPTCHA Component > ASP.NET > DotNET 4.0 Web Applications > Run
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www. w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>BotDetect CAPTCHA ASP.NET Validator Sample</title> <link type="text/css" rel="Stylesheet" href="StyleSheet.css" /> </head> <body> <form id="form1" runat="server"> <h1>BotDetect CAPTCHA ASP.NET Validator Sample</h1> <fieldset> <legend>CAPTCHA Validator</legend> <p class="prompt">Name:</p> <asp:TextBox ID="NameTextBox" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="NameValidator" runat="server" ControlToValidate="NameTextBox" ErrorMessage="Your name is required" EnableClientScript="true" SetFocusOnError="true"> Missing name</asp:RequiredFieldValidator> <p class="prompt"><label for="CaptchaCodeTextBox">Retype the characters from the picture:</label></p> <BotDetect:Captcha ID="SampleCaptcha" runat="server" /> <div class="validationDiv"> <asp:TextBox ID="CaptchaCodeTextBox" runat="server"></asp: TextBox> <BotDetect:CaptchaValidator ID="SampleCaptchaValidator" runat="server" ControlToValidate="CaptchaCodeTextBox" CaptchaControl="SampleCaptcha" ErrorMessage="Retype the characters exactly as they appear in the picture" EnableClientScript="true" SetFocusOnError="true">Incorrect CAPTCHA code</BotDetect:CaptchaValidator><br /> <asp:Label ID="ValidationPassedLabel" runat="server" CssClass="correct" Visible="False" Text="Validation passed!" />< br /> <asp:Button ID="SumbitButton" runat="server" Text="Submit" CausesValidation="true" /> </div> </fieldset> </div> </form> </body> </html>
Instead of validating the user's Captcha code input in page code-behind, we add a <BotDetect:CaptchaValidator> control to the page, and wire it up to process Captcha validation attempts by specifying the ControlToValidate (user input textbox) and CaptchaControl (Captcha instance) attributes.
Default.aspx.cs
using System; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_PreRender(object sender, EventArgs e) { // setup client-side input processing SampleCaptcha.UserInputClientID = CaptchaCodeTextBox.ClientID; if (IsPostBack) { // clear previous user input CaptchaCodeTextBox.Text = null; if (Page.IsValid) { ValidationPassedLabel.Visible = true; } else { ValidationPassedLabel.Visible = false; } } } }
Since the CaptchaValidator is active on the page, we can simply use Page.IsValid checks around protected code, and Captcha validation will be performed automatically during Page validation.
Web.config
<?xml version="1.0"?> <configuration xmlns="http://schemas.microsoft.com/. NetConfiguration/v2.0"> <system.web> <httpHandlers> <!-- Register the HttpHandler used for BotDetect Captcha requests --> <add verb="GET" path="BotDetectCaptcha.ashx" type="BotDetect.Web.CaptchaHandler, BotDetect"/> </httpHandlers> <!-- Register a custom SessionIDManager for BotDetect Captcha requests --> <sessionState mode="InProc" cookieless="AutoDetect" timeout="20" sessionIDManagerType="BotDetect.Web.CustomSessionIdManager, BotDetect"/> <!-- Session state is required for BotDetect storage; you can also turn if off globally and only enable for BotDetect-protected pages if you prefer --> <pages enableSessionState="true"> <controls> <!-- Register the BotDetect tag prefix for easier use in all pages --> <add assembly="BotDetect" namespace="BotDetect.Web.UI" tagPrefix="BotDetect"/> </controls> </pages> <compilation debug="true" targetFramework="4.0"> <assemblies> <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> </assemblies> </compilation> <trace enabled="false" localOnly="true"/> <httpCookies httpOnlyCookies="true"/> <trust level="Medium" originUrl=""/> <authentication mode="None"/> <customErrors mode="RemoteOnly"></customErrors> </system.web> <system.webServer> <validation validateIntegratedModeConfiguration="false"/> <handlers> <!-- Register the HttpHandler used for BotDetect Captcha requests (IIS 7.0+) --> <remove name="BotDetectCaptchaHandler"/> <add name="BotDetectCaptchaHandler" preCondition="integratedMode" verb="GET" path="BotDetectCaptcha.ashx" type="BotDetect.Web.CaptchaHandler, BotDetect"/> </handlers> </system.webServer> </configuration>
There are several BotDetect-related changes in the web.config file, as explained in the Configure Your ASP.NET Application to Use BotDetect CAPTCHA how to guide.
Visual Studio 2008 / .NET 3.5
By default, the .NET 3.5 C# version of the ASP.NET Captcha Validator sample project is installed at:
C:\Program Files\Lanapsoft\BotDetect 3 CAPTCHA Component\Asp.Net\v3.5\WebApp\AspNetValidatorCaptchaSample\CSharp
You can also run it from the BotDetect Start Menu:
Programs > Lanapsoft > BotDetect 3 CAPTCHA Component > ASP.NET > DotNET 3.5 Web Applications > Run
The Visual Studio 2008 / .NET 3.5 source has no essential differences from the Visual Studio 2010 / .NET 4.0 source.
Visual Studio 2005 / .NET 2.0
By default, the .NET 2.0 C# version of the ASP.NET Captcha Validator sample project is installed at:
C:\Program Files\Lanapsoft\BotDetect 3 CAPTCHA Component\Asp.Net\v2.0\WebApp\AspNetValidatorCaptchaSample\CSharp
You can also run it from the BotDetect Start Menu:
Programs > Lanapsoft > BotDetect 3 CAPTCHA Component > ASP.NET > DotNET 2.0 Web Applications > Run
The Visual Studio 2005 / .NET 2.0 source has no essential differences from the Visual Studio 2010 / .NET 4.0 source.
Current BotDetect Versions
- BotDetect PHP CAPTCHA v3.0.Alpha12012–02–06
- BotDetect ASP.NET CAPTCHA v3.0.92011–11–21
- BotDetect ASP Classic CAPTCHA v3.0.92011–11–21





