BotDetect ASP.NET MVC 6 CAPTCHA Integration Quickstart

1. Add CAPTCHA.asp.net.core.on.legacy.frameworks NuGet Package

  • Open the NuGet Package Manager.

    BotDetect ASP.NET Core Add Nuget Package
  • Choose nuget.org package source and search for CAPTCHA.asp.net.core.on.legacy.net.frameworks.

    BotDetect ASP.NET Core Nuget Package Description
  • Install the CAPTCHA.asp.net.core.on.legacy.net.frameworks package. This should add a line to you project.json similar to the one shown below.

    "dependencies": {
        ...
        "CAPTCHA.asp.net.core.on.legacy.net.frameworks": "4.2.0"
      }
    

2. Configure Your ASP.NET Application

Open Startup.cs and update it in the way demonstrated in the following:

  • Locate the ConfigureServices method
    public void ConfigureServices(IServiceCollection services)
    {
      services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
      services.AddMemoryCache(); // Adds a default in-memory implementation of 
                             // IDistributedCache
      // Add framework services.
      services.AddMvc();
      services.AddSession(options =>
      {
        options.IdleTimeout = TimeSpan.FromMinutes(20);
      });
    }
    
  • Locate the Configure method and add a call to app.UseCaptcha.

    public void Configure(IApplicationBuilder app, 
    IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
      ...
      app.UseSession();
      
      //configure BotDetectCaptcha
      app.UseCaptcha(Configuration);
      ...
    }
    
  • Add @addTagHelper directive makes Tag Helpers available to the view. To expose BotDetect Captcha Tag Helper in your project, you would use the following in Views/_ViewImports.cshtml file:

    @using AspNetCoreWebApplicationExample
    @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    @addTagHelper "BotDetect.Web.Mvc.CaptchaTagHelper, BotDetect.Web.Mvc"
    

3. Show a Captcha Challenge on the Form

In View code: import the BotDetect namespace, include BotDetect styles in page <head>, create a MvcCaptcha object and pass it to the Captcha TagHelper:
@using BotDetect.Web.Mvc
[…]
@section header {
    <environment names="Development,Staging,Production">
        <link href="@BotDetect.Web.CaptchaUrls.Absolute.LayoutStyleSheetUrl"
              rel="stylesheet" type="text/css" />
[…]
}

[…]

<label asp-for="CaptchaCode">Retype the code from the picture:</label>
@{ var exampleCaptcha = new MvcCaptcha("ExampleCaptcha") 
{ UserInputID = "CaptchaCode" }; }
<captcha mvc-captcha="exampleCaptcha" />
<div class="actions">
  <input asp-for="CaptchaCode" />
  <input type="submit" value="Validate" />
  <span asp-validation-for="CaptchaCode"></span>
  @if (ViewContext.IsPost() && ViewData.ModelState.IsValid)
  {
    <span class="correct">Correct!</span>
  }
</div>

[…]

4. Check User Input During Form Submission

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  
	MvcCaptcha.ResetCaptcha("ExampleCaptcha");			
    }  

In-Depth ASP.NET MVC CAPTCHA Instructions and Explanations

Detailed ASP.NET MVC Captcha instructions and explanations can be found in the ASP.NET MVC Captcha integration how to guide.

Other BotDetect ASP.NET CAPTCHA Quickstarts

ASP.NET WebForms CAPTCHA.

ASP.NET Web Pages Basic CAPTCHA.