BotDetect CAPTCHA ASP.NET Code Examples

First Time Here?

Check the BotDetect Developer Crash Course for key integration steps.

The BotDetect ASP.NET Captcha installations include 22 example projects which can help you get started with integrating BotDetect in your applications and configuring various Captcha options.

Samples are divided by the .NET runtime version used, and each example comes in both C# and VB.NET versions.

Table of Contents

BotDetect CAPTCHA ASP.NET Code Examples Installation

When you install the BotDetect ASP.NET Captcha control, you'll be given a choice of features to install:

BotDetect ASP.NET Setup feature selection

Note that:

  • By default, all features are selected
  • You can choose which .NET runtime versions you want to support. Each of the options has separate BotDetect assemblies, configuration files and web applications.
  • Selecting Examples under each .NET version installs the BotDetect web application files – ASP.NET code examples source code.
  • Selecting Deploy to IIS under each .NET versions deploys the BotDetect web applications to localhost, creating IIS applications and application pools running the selected .NET runtime version.
  • If setup detects that you don't have IIS and the required .NET Framework version installed on the machine, the Deploy to IIS option won't be shown.

When the web applications are installed, you can view their source code in the BotDetect installation folder and run them using the provided Start Menu shortcuts:

.NET Version Default Installation Folder Start Menu Shortcut
.NET 4.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\ Asp.Net\.NET4.5\WebApp Start -> Programs -> Captcha Inc -> BotDetect 4 CAPTCHA Component -> ASP.NET -> ASP.NET 4.5 Examples
.NET 4.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\ Asp.Net\.NET4.0\WebApp Start -> Programs -> Captcha Inc -> BotDetect 4 CAPTCHA Component -> ASP.NET -> ASP.NET 4.0 Examples
.NET 3.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\ Asp.Net\.NET3.5\WebApp Start -> Programs -> Captcha Inc -> BotDetect 4 CAPTCHA Component -> ASP.NET -> ASP.NET 3.5 Examples
.NET 2.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\ Asp.Net\.NET2.0\WebApp Start -> Programs -> Captcha Inc -> BotDetect 4 CAPTCHA Component -> ASP.NET -> ASP.NET 2.0 Examples

BotDetect CAPTCHA ASP.NET Developer Crash Course

Be sure to check our Developer Crash Course to quickly learn the few key integration steps.

BotDetect ASP.NET WebForms CAPTCHA Integration Examples

These code examples show how to integrate BotDetect in your ASP.NET WebForms applications.

ASP.NET Basic Captcha Example

This example project shows the most basic source code required to protect an ASP.NET form with BotDetect Captcha and validate the user input.

It can be used as a starting point when you are first learning how to use BotDetect.

.NET Version Default Source Code Folder Online Source
.NET 4.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\AspNetWebFormsBasicCaptchaExample
.NET 4.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.0\WebApp\AspNetBasicCaptchaExample
.NET 3.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET3.5\WebApp\AspNetBasicCaptchaExample
.NET 2.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET2.0\WebApp\AspNetBasicCaptchaExample

ASP.NET Custom Captcha Validator Example

This example project shows how to use the CaptchaValidator control to integrate BotDetect Captcha validation with standard ASP.NET page validation functionality and other validator controls.

.NET Version Default Source Code Folder Online Source
.NET 4.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\AspNetWebFormsValidatorCaptchaExample
.NET 4.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.0\WebApp\AspNetValidatorCaptchaExample
.NET 3.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET3.5\WebApp\AspNetValidatorCaptchaExample
.NET 2.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET2.0\WebApp\AspNetValidatorCaptchaExample

ASP.NET Membership Captcha Example

This example project shows how to integrate BotDetect Captcha validation with standard ASP.NET Membership functionality used in ASP.NET Login and CreateUserWizard controls.

.NET Version Default Source Code Folder Online Source
.NET 4.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ AspNetMembershipCaptchaExample
.NET 4.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.0\WebApp\ AspNetMembershipCaptchaExample
.NET 3.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET3.5\WebApp\ AspNetMembershipCaptchaExample
.NET 2.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET2.0\WebApp\ AspNetMembershipCaptchaExample

ASP.NET 4.5 Application Template Captcha Example

This example project shows how to add BotDetect CAPTCHA protection to the registration form included in the default ASP.NET 4.5 Web Forms Application project template coming with Visual Studio 2012.

Since the Register form uses a CreateUserWizard, the sample shows how to include BotDetect CAPTCHA validation in new user data validation.

The example also shows how to complement server-side CAPTCHA validation with client-side Ajax CAPTCHA validation using ASP.NET 4.5 unobtrusive validation applied to all form fields.

Visual Studio Default Source Code Folder Online Source
Visual Studio 2013 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ AspNetWebFormsVS2013AppTemplateExample
Visual Studio 2012 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ AspNetWebFormsVS2012AppTemplateExample

ASP.NET Ajax Captcha Example

This example project shows how to use BotDetect within an ASP.NET Ajax UpdatePanel. The basic code used to add Captcha protection to the page is the same as in regular ASP.NET forms.

However, some additional work is required if BotDetect is not visible on the first page load, but gets added to the page dynamically.

ASP.NET Ajax Version Default Source Code Folder Online Source
ASP.NET Ajax 4.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ AspNetWebFormsAjaxCaptchaExample
ASP.NET Ajax 4.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.0\WebApp\ AspNetAjax40CaptchaExample
ASP.NET Ajax 1.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET3.5\WebApp\ AspNetAjax10CaptchaExample

ASP.NET jQuery Validation Captcha Example

This example project shows how to integrate BotDetect ASP.NET Captcha validation with jQuery Validation client-side form validation.

Client-side validation is not secure by itself (it can be bypassed trivially by bots that don't execute JavaScript at all), so the example shows how the protected form action must always be secured by server-side Captcha validation first, and uses client-side validation only to improve the user experience.

.NET Version Default Source Code Folder Online Source
.NET 4.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ AspNetWebFormsJQueryValidationCaptchaExample
.NET 4.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.0\WebApp\ AspNetJQueryValidationCaptchaExample
.NET 3.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET3.5\WebApp\ AspNetJQueryValidationCaptchaExample
.NET 2.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET2.0\WebApp\ AspNetJQueryValidationCaptchaExample

BotDetect ASP.NET MVC CAPTCHA Integration Examples

These code examples show how to integrate BotDetect in your ASP.NET MVC applications.

ASP.NET MVC Basic CAPTCHA Example

The ASP.NET MVC Basic Captcha example project shows the most basic source code required to protect an ASP.NET MVC form with BotDetect CAPTCHA and validate the user input.

ASP.NET MVC View code displaying CAPTCHA protection can be found in Views/Sample/Index.aspx, and the ASP.NET MVC Controller code checking user input is in Controllers/SampleController.vb.

ASP.NET MVC Default Source Code Folder Online Source
ASP.NET MVC 5.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ AspNetMvc50BasicCaptchaExample
ASP.NET MVC 4.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ AspNetMvc40BasicCaptchaExample

ASP.NET MVC Application Template CAPTCHA Example

This example project shows how to use the BotDetect MvcCaptcha control in ASP.NET MVC web applications.

Starting with the default ASP.NET MVC example project, the example includes all code required to add Captcha validation to the Account controller Register action.

ASP.NET MVC Default Source Code Folder Online Source
ASP.NET MVC 5.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ AspNetMvc50ApplicationTemplateCaptchaExample
ASP.NET MVC 4.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ AspNetMvc40CaptchaExample
ASP.NET MVC 3.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.0\WebApp\ AspNetMvc30CaptchaExample
ASP.NET MVC 2.0

C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.0\WebApp\ AspNetMvc20CaptchaExample

and

C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET3.5\WebApp\ AspNetMvc20CaptchaExample

ASP.NET MVC 1.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET3.5\WebApp\ AspNetMvc10CaptchaExample

ASP.NET MVC jQuery Ajax CAPTCHA Example

The ASP.NET MVC jQuery Ajax Captcha VB.NET ASPX example project shows how to perform Ajax Captcha validation using a jQuery $.getJSON call on the client combined with an ASP.NET MVC JsonResult Controller action on the server.

Both the client-side code (located in Scripts\CheckCaptcha.js) and the server-side code (Controllers\SampleController.vb) are very simple, and allow BotDetect Captcha protection to be added in a variety of custom Ajax forms and scenarios.

ASP.NET MVC Default Source Code Folder Online Source
ASP.NET MVC 5.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ AspNetMvc50AjaxCaptchaExample
ASP.NET MVC 4.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ AspNetMvc40AjaxCaptchaExample

BotDetect ASP.NET CAPTCHA Configuration Examples

Captcha configuration code examples show how to use particular BotDetect Captcha options in your ASP.NET applications

ASP.NET Captcha Application Config Settings Example

This example shows how to configure Captcha challenges by overriding Captcha control defaults in application configuration files.

BotDetect allows user-defined customization of many Captcha options through a special <botDetect> section of the web.config file.

Captcha settings from this configuration file will apply to all Captcha challenges shown on forms in the applications, and will act as defaults with which all Captcha objects will be created. This makes configuration file settings the simplest and most convenient way of Captcha customization for most use cases.

.NET Version Default Source Code Folder Online Source
.NET 4.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ CaptchaApplicationConfigSettingsExample
.NET 4.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.0\WebApp\ CaptchaApplicationConfigSettingsExample
.NET 3.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET3.5\WebApp\ CaptchaApplicationConfigSettingsExample
.NET 2.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET2.0\WebApp\ CaptchaApplicationConfigSettingsExample

ASP.NET Captcha ClientSide Workflow Settings Example

This BotDetect Captcha ASP.NET code example shows how to use custom BotDetect client-side events to execute user-defined JavaScript code at various stages of the Captcha challenge workflow.

Client-side Captcha object initialization, Captcha image reloading, Captcha sound playback, built-in Captcha Ajax validation, and Captcha help link clicks all have a number of related client-side "events" and hooks where user-defined client-side callbacks can be injected.

.NET Version Default Source Code Folder Online Source
.NET 4.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ CaptchaClientSideWorkflowSettingsExample
.NET 4.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.0\WebApp\ CaptchaClientSideWorkflowSettingsExample
.NET 3.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET3.5\WebApp\ CaptchaClientSideWorkflowSettingsExample
.NET 2.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET2.0\WebApp\ CaptchaClientSideWorkflowSettingsExample

ASP.NET Captcha Form Object Settings Example

This BotDetect Captcha ASP.NET code example shows how to configure Captcha challenges by setting Captcha control properties in ASP.NET form source.

Multiple ASP.NET forms within the same ASP.NET website can be protected by BotDetect Captcha challenges: e.g. you could add Captcha controls in both your Contact form and Registration form source.

.NET Version Default Source Code Folder Online Source
.NET 4.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ CaptchaFormObjectSettingsExample
.NET 4.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.0\WebApp\ CaptchaFormObjectSettingsExample
.NET 3.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET3.5\WebApp\ CaptchaFormObjectSettingsExample
.NET 2.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET2.0\WebApp\ CaptchaFormObjectSettingsExample

ASP.NET Captcha Request Dynamic Settings Example

This BotDetect Captcha ASP.NET code example shows how to dynamically adjust Captcha configuration, potentially on each Http request made by the client.

Any code setting Captcha properties in the Captcha.InitializedWebCaptcha event handler will be executed not only for each protected form GET or POST request (like Captcha configuration code placed in form source would be), but also for each each GET request loading a Captcha image or sound, or making an Ajax Captcha validation call.

.NET Version Default Source Code Folder Online Source
.NET 4.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ CaptchaRequestDynamicSettingsExample
.NET 4.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.0\WebApp\ CaptchaRequestDynamicSettingsExample
.NET 3.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET3.5\WebApp\ CaptchaRequestDynamicSettingsExample
.NET 2.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET2.0\WebApp\ CaptchaRequestDynamicSettingsExample

BotDetect ASP.NET CAPTCHA Web Pages Example

ASP.NET CATCHA Web Pages Example

This example project shows the most basic source code required to protect an ASP.NET Web Pages form with BotDetect CAPTCHA and validate the user input.

ASP.NET Web Pages code displaying CAPTCHA protection and checking user input can be found in Index.cshtml.

.NET Version Default Source Code Folder Online Source
.NET 4.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\ AspNetWebPagesBasicCaptchaExample

BotDetect ASP.NET CAPTCHA Troubleshooting Examples

Captcha options code example projects show how to use particular BotDetect Captcha options in your ASP.NET applications

ASP.NET CATCHA Built-In Troubleshooting Example

This example project shows how to use BotDetect built-in error logging and Captcha event tracing, using the BotDetect Troubleshooting utility based on log4net.

Such logging techniques can be used as a foundation for effective diagnosis and resolution of any BotDetect issues you might encounter on your servers.

.NET Version Default Source Code Folder Online Source
.NET 4.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\CaptchaTroubleshootingExample
.NET 4.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.0\WebApp\CaptchaTroubleshootingExample
.NET 3.5 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET3.5\WebApp\CaptchaTroubleshootingExample
.NET 2.0 C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET2.0\WebApp\CaptchaTroubleshootingExample