How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005 (BotDetect v2.0)

Please Note

The information on this page is out of date and applies to a deprecated version of BotDetect™ CAPTCHA (v2.0).

An up-to-date equivalent page for the latest BotDetect Captcha release (v3) is BotDetect v3 ASP.NET Captcha How To guides.

General information about the major improvements in the current BotDetect release can be found at the What's New in BotDetect v3.0 page.

This guide demonstrated how to use BotDetect ASP.NET CAPTCHA in a new Visual Studio 2005 Web Site project.

Step 1. Create new ASP.NET 2.0 Web Site

  • Start Visual Studio 2005

How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005: screenshot 1

  • Select the project location, name and programming language used

How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005: screenshot 2

Step 2. Add a BotDetect reference to the project

  • In the Solution Explorer, right-click the Web Site project, and Choose "Add Reference"

How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005: screenshot 3

  • Browse to the Lanap.BotDetect.dll file located in the BotDetect CAPTCHA installation folder

How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005: screenshot 4

  • The reference is added to the project's Bin folder

How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005: screenshot 5

Step 3. Configure your site to use BotDetect CAPTCHA

  • In the Solution Explorer, right-click the Web Site project, and Choose "Add New Item"

How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005: screenshot 6

  • Add a new Configuration File to the project

How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005: screenshot 7

  • Add the following lines to the <system.web> section of the newly created "Web.config" file:
<httpHandlers>
  <add verb="*" path="LanapCaptcha.aspx" 
    type="Lanap.BotDetect.CaptchaHandler, Lanap.BotDetect" />
</httpHandlers>

<sessionState mode="InProc" cookieless="AutoDetect" 
  timeout="20" sessionIDManagerType="
    Lanap.BotDetect.Persistence.CustomSessionIDManager, 
    Lanap.BotDetect" />

How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005: screenshot 8

  • If you want to deploy your project on servers running IIS 7.0 in integrated mode, you will also have to add a <system.webServer> element at the bottom of the <configuration> element, with the following content:
<system.webServer>
  <validation validateIntegratedModeConfiguration="false"/>
  <handlers>
    <remove name="LanapCaptchaHandler" />
    <add name="LanapCaptchaHandler" 
      preCondition="integratedMode" verb="*" 
      path="LanapCaptcha.aspx" 
      type="Lanap.BotDetect.CaptchaHandler, Lanap.BotDetect" />
  </handlers>
</system.webServer>

Step 4. Add BotDetect CAPTCHA to the Visual Studio Toolbox for future reference.

  • Right-click anywhere in the Toolbox and select "Choose Items"

How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005: screenshot 9

  • Once again, browse to the Lanap.BotDetect.dll file located in the BotDetect CAPTCHA installation folder

How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005: screenshot 10

Step 5. Add the BotDetect CAPTCHA control to a page

  • Drag the newly added Captcha control from the Toolbox to the Default.aspx page

How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005: screenshot 11

How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005: screenshot 12

  • Save all files, then compile and run the project. You will see a CAPTCHA image rendered on your web form.

How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005: screenshot 13

Step 6. Add user input validation logic

  • Add a TextBox, a Button, and a Label server control to the page

How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005: screenshot 14

  • Rename the controls and set the page layout - the .aspx file fragment should look like:

    <div>
      <BotDetect:Captcha ID="SampleCaptcha" runat="server" />
    </div>
    <div>
      <asp:TextBox ID="CodeTextBox" runat="server"></asp:TextBox>
      <asp:Button ID="ValidateButton" Text="Validate" />
      <asp:Label ID="MessageLabel" runat="server"></asp:Label>
    </div>
  • Add the following code to the Page_Load handler in the page codebehind:

    protected void Page_Load(object sender, EventArgs e)
    {
      if (IsPostBack)
      {
        string code = CodeTextBox.Text.Trim().ToUpper();
    	
        if (SampleCaptcha.Validate(code))
        {
          MessageLabel.Text = "Correct";
        }
        else
        {
          MessageLabel.Text = "Incorrect";
        }
    
        CodeTextBox.Text = null;
      }
    }
  • Save all files, then compile and run the project. You can then try CAPTCHA validation in action

How To use BotDetect ASP.NET CAPTCHA in Visual Studio 2005: screenshot 17

  • On production web sites you will typically change the validation code to redirect the user to the resource requested if CAPTCHA validation succeeds

Sample BotDetect CAPTCHA project source code

You can find the full source code similar to the result you should get when following these instructions in the sample project coming with the BotDetect CAPTCHA installation.


Please Note

The information on this page is out of date and applies to a deprecated version of BotDetect™ CAPTCHA (v2.0).

An up-to-date equivalent page for the latest BotDetect Captcha release (v3) is BotDetect v3 ASP.NET Captcha How To guides.

General information about the major improvements in the current BotDetect release can be found at the What's New in BotDetect v3.0 page.

language: English Español Tiếng Việt