ASP.NET Web Pages Basic CAPTCHA VB.NET 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.vbhtml.

Download the BotDetect ASP.NET CAPTCHA Generator archive to run this example
  • C#
  • VB.NET

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/vbnet/ folder; and contains the following files:

Index.vbhtml

@* namespace needed to access BotDetect members *@
@Imports 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")
      @Code
        Dim exampleCaptcha As Captcha = New Captcha("ExampleCaptcha")
        exampleCaptcha.UserInputID = "CaptchaCode"
      End Code
      @Html.Raw(exampleCaptcha.Html)
      <div class="validationDiv">
        @Html.TextBox("CaptchaCode")
        <input type="submit" value="Validate" />
        @If ((Request.HttpMethod = "POST")) Then
          Dim isHuman As Boolean = exampleCaptcha.Validate()
          If (isHuman) Then
          @<span class="correct">Correct!</span>
          Else
          @<span class="incorrect">Incorrect!</span>
          End If
        End If
      </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="false" 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>