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 asp.net 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

Default.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>BotDetect ASP.NET CAPTCHA Options: Application Config Settings Code 
  Example</title>

  <script type="text/javascript" 

</head>
<body>
  <form runat="server" class="column" id="form1">
    <h1>BotDetect ASP.NET CAPTCHA Options:
      <br />
      Application Config Settings Code Example</h1>
    <fieldset>
      <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" 
        CssClass="incorrect"></asp:Label>
      </div>
    </fieldset>
  </form>
</body>

Default.aspx.cs

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;
      }
      else
      {
        CaptchaCorrectLabel.Visible = false;
        CaptchaIncorrectLabel.Visible = true;
      }
    }
  }
}

Web.config

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please 
  visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
  </appSettings>
  <system.web>
  <httpHandlers>
    <!-- Register the HttpHandler used for BotDetect Simple API requests -->
    <add verb="GET" path="CaptchaCustomPath.ashx" 
    type="BotDetect.Web.SimpleCaptchaHandler, BotDetect"/>
  </httpHandlers>
  <pages controlRenderingCompatibilityVersion="4.5" enableSessionState="true">
    <controls>
    <!-- Register the BotDetect tag prefix for easier use in all pages -->
    <add assembly="BotDetect" namespace="BotDetect.Web.UI" 
    tagPrefix="BotDetect"/>
    </controls>
  </pages>
  <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"/>
  </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="CaptchaCustomPath.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>MemcachedCaptchaExample</name>
      <userInputID>CaptchaCodeTextBox</userInputID>
    </captchaStyle>
  </captchaStyles>
  
  
  <captchaPersistence>
    <persistenceType>Memcached</persistenceType>
    <address>127.0.0.1:11211</address>
  </captchaPersistence>
  
</botdetect>

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