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 Component and run this example

Visual Studio 2015 / Visual Studio 2013 / .NET 4.6 / .NET 4.5

By default, the .NET 4.5 C# version of the ASP.NET Web Pages basic Captcha example project is installed at:
C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET4.5\WebApp\AspNetWebPagesBasicCaptchaExample\CSharp

You can also run it from the BotDetect Start Menu:
Programs > Captcha Inc > BotDetect 4 CAPTCHA Component > ASP.NET > DotNET 4.5 Web Applications > Run


@* 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" />