BotDetect ASP.NET Simple CAPTCHA Options: Server Side Persistence in Memcached Database C# Code Example

The ASP.NET Simple Captcha options: Server side persistence in Memcached database code example shows how to use Memcached to store persist Captcha data instead of using default BotDetect SQLite Database persistence provider.

First Time Here?

Check the BotDetect Developer Crash Course for key integration steps.

As you may know Memcached is an open source, in-memory data structure store used to improve data performance. Therefore Captcha data storage via Memcached is neccessary in real world. This combination makes your application have better performance.

To use Memcached to store persist Captcha data, BotDetect ASP.NET Simple Captcha provides you a simple way by declaring Memcached configuration info in botdetect.xml file, such as: memcached host, port.

BotDetect ASP.NET Simple Captcha uses EnyimMemcached as Memcached client, so you need to ensure EnyimMemcached NuGet package was installed.

Download the BotDetect ASP.NET CAPTCHA Library and run this example

Installed Location

By default, the ASP.NET SimpleCaptcha persistence settings example project is installed at:
C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET\WebApp\SimpleAPI\ CaptchaServerSidePersistenceInMemcachedDatabaseExample\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


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="">
<head id="Head1" runat="server">
  <title>BotDetect ASP.NET CAPTCHA Options: Application Config Settings Code 

  <script type="text/javascript" 

  <form runat="server" class="column" id="form1">
    <h1>BotDetect ASP.NET CAPTCHA Options:
      <br />
      Application Config Settings Code Example</h1>
      <legend>ASP.NET WebForm CAPTCHA Validation</legend>
      <p class="prompt">
        <label for="CaptchaCodeTextBox">Retype the characters from the picture:</label</p>
      <BotDetect:WebFormsSimpleCaptcha runat="server" ID="MemcachedCaptchaExample"/>
      <div class="validationDiv">
        <asp:TextBox ID="CaptchaCodeTextBox" runat="server"></asp:TextBox>
        <asp:Button ID="ValidateCaptchaButton" runat="server" />
        <asp:Label ID="CaptchaCorrectLabel" runat="server" CssClass="correct"></asp:Label>
        <asp:Label ID="CaptchaIncorrectLabel" runat="server" 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
  protected void Page_PreRender(object sender, EventArgs e)
    // initial page setup
    if (!IsPostBack)
      // set control text
      ValidateCaptchaButton.Text = "Validate";
      CaptchaCorrectLabel.Text = "Correct!";
      CaptchaIncorrectLabel.Text = "Incorrect!";

      // these messages are shown only after validation
      CaptchaCorrectLabel.Visible = false;
      CaptchaIncorrectLabel.Visible = false;

    if (IsPostBack)
      // validate the Captcha to check we're not dealing with a bot
      bool isHuman = MemcachedCaptchaExample.Validate();

      if (isHuman)
        CaptchaCorrectLabel.Visible = true;
        CaptchaIncorrectLabel.Visible = false;
        CaptchaCorrectLabel.Visible = false;
        CaptchaIncorrectLabel.Visible = true;


<?xml version="1.0"?>
  For more information on how to configure your ASP.NET application, please 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
    <!-- Register the HttpHandler used for BotDetect Simple API requests -->
    <add verb="GET" path="CaptchaCustomPath.ashx" 
    type="BotDetect.Web.SimpleCaptchaHandler, BotDetect"/>
  <pages controlRenderingCompatibilityVersion="4.5" enableSessionState="true">
    <!-- Register the BotDetect tag prefix for easier use in all pages -->
    <add assembly="BotDetect" namespace="BotDetect.Web.UI" 
  <compilation debug="false" targetFramework="4.5"/>
  <httpRuntime requestValidationMode="4.5" targetFramework="4.5" 
  encoderType="System.Web.Security.AntiXss.AntiXssEncoder, System.Web, Version=4.
  0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
  <machineKey compatibilityMode="Framework45"/>
  <validation validateIntegratedModeConfiguration="false"/>
    <!-- Register the HttpHandler used for BotDetect Simple API requests (IIS 7.0+) -->
    <remove name="BotDetectCaptchaHandler"/>
    <add name="BotDetectCaptchaHandler" preCondition="integratedMode" verb="GET" 
    type="BotDetect.Web.SimpleCaptchaHandler, BotDetect"/>


<?xml version="1.0" encoding="UTF-8"?>
<botdetect xmlns="" 


In botdetect.xml configuration file, we tell BotDetect to use Memcached as the BotDetect persistence provider instead of the default one (SQLite Database). You can find the detailed explanation about this at Captcha Memcached persistence provider documentation.

Current BotDetect Versions