ASP.NET Web Pages Basic CAPTCHA C# Code Example

The ASP.NET ASP.NET Web Pages basic Captcha Simple API 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 2017, 2015, 2013 / .NET 4.5 and onwards

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

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

Index.cshtml

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

<html lang="en">

<head>
  <meta charset="utf-8" />
  <title>BotDetect ASP.NET CAPTCHA Validation: Basic ASP.NET Web Pages CAPTCHA 
  Code Example</title>
</head>

<body>
  <form method="post" class="column">
    <h1>BotDetect ASP.NET CAPTCHA Validation: <br /> Basic ASP.NET Web Pages 
    CAPTCHA Code Example</h1>
    <fieldset>
      <legend>ASP.NET Web Pages CAPTCHA Validation</legend>
      @Html.Label("Retype the code from the picture:", "CaptchaCode")
      @{
        SimpleCaptcha exampleCaptcha = new SimpleCaptcha("ExampleCaptcha");

      }
      @Html.Raw(exampleCaptcha.Html)
      <div class="validationDiv">
        @Html.TextBox("CaptchaCode")
        <input type="submit" value="Validate" />
        @if ((Request.HttpMethod == "POST"))
        {
          bool isHuman = exampleCaptcha.Validate();
          if (isHuman)
          {
            <span class="correct">Correct!</span>
          }
          else
          {
            <span class="incorrect">Incorrect!</span>
          }
        }
      </div>
    </fieldset>
  </form>
</body>
</html>

Web.config

<?xml version="1.0"?>
<configuration>
  <appSettings>
  <add key="webPages:Version" value="2.0"/>
  </appSettings>
  <system.web>
  <compilation debug="false" targetFramework="4.0"/>
  <httpHandlers>
    <!-- Register the HttpHandler used for BotDetect Simple API requests -->
    <add verb="GET" path="BotDetectCaptcha.ashx" 
    type="BotDetect.Web.SimpleCaptchaHandler, BotDetect"/>
  </httpHandlers>
  </system.web>
  <system.webServer>
  <validation validateIntegratedModeConfiguration="false"/>
  <handlers>
    <!-- Register the HttpHandler used for BotDetect Simple API requests (IIS 7.0+) -->
    <remove name="BotDetectCaptchaHandler"/>
    <add name="BotDetectCaptchaHandler" preCondition="integratedMode" verb="GET" 
    path="BotDetectCaptcha.ashx" type="BotDetect.Web.SimpleCaptchaHandler, BotDetect"/>
  </handlers>
  </system.webServer>
 </configuration>

botdetect.xml

<?xml version="1.0" encoding="UTF-8"?>
<botdetect xmlns="https://captcha.com/schema/net" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://captcha.com/schema/net 
      https://captcha.com/schema/net/botdetect-4.4.0.xsd">

  <captchaStyles>
    <captchaStyle>
      <name>ExampleCaptcha</name>
      <userInputID>CaptchaCode</userInputID>
      <codeLength>3-5</codeLength>
    </captchaStyle>
  </captchaStyles>

</botdetect>

In botdetect.xml, we configure captcha options for the ExampleCaptcha captcha style name. You can find a full list of available Captcha configuration options and related instructions at the Captcha configuration options page .