The BotDetect CAPTCHA NuGet Package

The free version of BotDetect ASP.NET Captcha is also available as a NuGet package in the www.nuget.org NuGet Gallery: CAPTCHA NuGet package.

To install CAPTCHA using NuGet, right-click your project in Visual Studio, select "Manage NuGet Packages…" and search for "BotDetect", or run the following command in the Package Manager Console:

PM> Install-Package Captcha

After adding the Captcha NuGet package to your application, check the following resources:

NuGet CAPTCHA Integration Instructions

To show a Captcha challenge on the form and check user input during form submission:

NuGet CAPTCHA Integration into ASP.NET MVC Applications

In View code: import the BotDetect namespace, include BotDetect styles in page , create a MvcCaptcha object and pass it to the Captcha HtmlHelper:

@using BotDetect.Web.Mvc;

  […]

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

  […]

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

Exclude BotDetect paths from ASP.NET MVC Routing:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // BotDetect requests must not be routed
    routes.IgnoreRoute("{*botdetect}", 
      new { botdetect = @"(.*)BotDetectCaptcha\.ashx" });

Mark the protected Controller action with the CaptchaValidation attribute to include Captcha validation in ModelState.IsValid checks:

using BotDetect.Web.Mvc;
  
  […]

[HttpPost]
[AllowAnonymous]
[CaptchaValidation("CaptchaCode", "ExampleCaptcha", "Incorrect CAPTCHA code!")]
public ActionResult ExampleAction(ExampleModel model)
{
    if (!ModelState.IsValid)
    {  
      // TODO: Captcha validation failed, show error message      
    }  
    else
    {  
      // TODO: Captcha validation passed, proceed with protected action  
    }  

Detailed ASP.NET MVC integration instructions can be found in the ASP.NET MVC Captcha integration guide.

NuGet CAPTCHA Integration into ASP.NET Web Pages Applications

On the ASP.NET form you want to protect against bots, add:

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

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>
    }
}

Detailed ASP.NET Web Pages integration instructions can be found in the ASP.NET Web Pages Captcha integration guide.

NuGet CAPTCHA Integration into ASP.NET WebForms Applications

On the ASP.NET form you want to protect against bots, add:

<BotDetect:Captcha ID="ExampleCaptcha" runat="server" />
<asp:TextBox ID="CaptchaCodeTextBox" runat="server" />

When the form is submitted, the Captcha validation result must be checked:

if (IsPostBack)
{
    // validate the Captcha to check we're not dealing with a bot
    bool isHuman = ExampleCaptcha.Validate(CaptchaCodeTextBox.Text);
    
    CaptchaCodeTextBox.Text = null; // clear previous user input

    if (!isHuman)
    {
      // TODO: Captcha validation failed, show error message  
    }
    else
    {
      // TODO: Captcha validation passed, proceed with protected action  
    }
}

Detailed ASP.NET WebForms integration instructions can be found in the ASP.NET WebForms Captcha integration guide.

BotDetect CAPTCHA Code Examples Download

Captcha code examples (ASP.NET WebForms integration examples, ASP.NET MVC integration examples, BotDetect configuration examples - with both C# and VB.NET and ASPX/Razor variants), as well as equivalents for older .NET framework versions and additional resources are included in the BotDetect setup package which can be downloaded from the BotDetect Captcha download page.

BotDetect ASP.NET Captcha Free Download

BotDetect CAPTCHA Documentation

The full index of available BotDetect ASP.NET CAPTCHA documentation can be found on the BotDetect documentation page.