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:

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> 
  <link href="@CaptchaUrls.Absolute.LayoutStyleSheetUrl" rel="stylesheet" 
  type="text/css" /> 
  <link href="StyleSheet.css" rel="stylesheet" type="text/css" /> 
</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") 
      @{ 
        Captcha exampleCaptcha = new Captcha("ExampleCaptcha"); 
        exampleCaptcha.UserInputID = "CaptchaCode"; 
      } 
      @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> 
  <configSections> 
  <section name="botDetect" requirePermission="false" 
  type="BotDetect.Configuration.BotDetectConfigurationSection, BotDetect"/> 
  </configSections> 
  <appSettings> 
  <add key="webPages:Version" value="2.0"/> 
  </appSettings> 
  <system.web> 
  <compilation debug="false" targetFramework="4.0"/> 
  <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"/> 
  </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> 
  <botDetect helpLinkEnabled="true" helpLinkMode="image" /> 
</configuration>