BotDetect ASP.NET WebPages CAPTCHA Integration Quickstart

1. Add a Reference to BotDetect

Run the following command in the Package Manager Console:

PM> Install-Package Captcha

After the command execution, the references to the BotDetect.dll, BotDetect.Web.MVC.dll, and System.Data.SQLite.dll assemblies will be added to your project.

If your project does not use the MVC, the BotDetect.Web.MVC.dll assembly reference can be safely removed from it.

If your project is not going to be using the Simple API, the System.Data.SQLite.dll assembly reference can be safely removed from it.

2. Show a Captcha Challenge on the Form

Import the BotDetect namespace, include BotDetect styles in page <head>, create a Captcha object and pass it to the Captcha HtmlHelper:
@using BotDetect.Web; 
  […]

  <link href="@BotDetect.Web.CaptchaUrls.Absolute.LayoutStyleSheetUrl"
    rel="stylesheet" type="text/css" />
  </head>

    […]

  
      @{
        Captcha exampleCaptcha = new Captcha("ExampleCaptcha");
        exampleCaptcha.UserInputID = "CaptchaCode";
      }
      @Html.Raw(exampleCaptcha.Html)
  @Html.TextBox("CaptchaCode")
  

3. Check User Input During Form Submission

When the form is submitted, the Captcha validation result must be checked:
  @if ((Request.HttpMethod == "POST"))
  {
    bool isHuman = exampleCaptcha.Validate();
    if (isHuman)
    {
      <span class="correct">Correct!</span>
    }
    else
    {
      <span class="incorrect">Incorrect!</span>
    }
  }

4. Configure Your ASP.NET Application

Update your application configuration (the web.config file):
<system.web>
  <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>

Other BotDetect ASP.NET CAPTCHA Quickstarts

ASP.NET WebForms Basic CAPTCHA.

ASP.NET MVC CAPTCHA.