ASP.NET Web Pages Basic CAPTCHA C# Code Example

The ASP.NET ASP.NET Web Pages basic Captcha 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.

First Time Here?

Check the BotDetect Developer Crash Course for key integration steps.

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

Download the BotDetect ASP.NET CAPTCHA Generator archive to run this example

Visual Studio 2017, 2015, 2013 / .NET 4.5 and onwards

Within this page, the root folder of the extracted archive is referred as the <BDC-DIR>.

This example is in the <BDC-DIR>/lgcy-on-lgcy/examples/t_api-captcha-webpages-api_basics/csharp/ folder; and contains the following files:


@* namespace needed to access BotDetect members *@
@using BotDetect.Web;
<!DOCTYPE html>

<html lang="en">

  <meta charset="utf-8" />
  <title>BotDetect ASP.NET CAPTCHA Validation: Basic ASP.NET Web Pages CAPTCHA 
  Code Example</title>
  <link href="@CaptchaUrls.Absolute.LayoutStyleSheetUrl" rel="stylesheet" 
  type="text/css" />
  <link href="StyleSheet.css" rel="stylesheet" type="text/css" />

  <form method="post" class="column">
    <h1>BotDetect ASP.NET CAPTCHA Validation: <br /> Basic ASP.NET Web Pages 
    CAPTCHA Code Example</h1>
      <legend>ASP.NET Web Pages CAPTCHA Validation</legend>
      @Html.Label("Retype the code from the picture:", "CaptchaCode")
        Captcha exampleCaptcha = new Captcha("ExampleCaptcha");
        exampleCaptcha.UserInputID = "CaptchaCode";
      <div class="validationDiv">
        <input type="submit" value="Validate" />
        @if ((Request.HttpMethod == "POST"))
          bool isHuman = exampleCaptcha.Validate();
          if (isHuman)
            <span class="correct">Correct!</span>
            <span class="incorrect">Incorrect!</span>


<?xml version="1.0"?>
  <section name="botDetect" requirePermission="false" 
  type="BotDetect.Configuration.BotDetectConfigurationSection, BotDetect"/>
  <add key="webPages:Version" value="2.0"/>
  <compilation debug="false" targetFramework="4.0"/>
    <!-- Register the HttpHandler used for BotDetect Captcha requests -->
    <add verb="GET" path="BotDetectCaptcha.ashx" 
    type="BotDetect.Web.CaptchaHandler, BotDetect"/>
  <!-- Register a custom SessionIDManager for BotDetect Captcha requests -->
  <sessionState mode="InProc" cookieless="AutoDetect" timeout="20" 
  sessionIDManagerType="BotDetect.Web.CustomSessionIdManager, BotDetect"/>
  <validation validateIntegratedModeConfiguration="false"/>
    <!-- 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"/>
  <botDetect helpLinkEnabled="true" helpLinkMode="image" />