How to implement Captcha in MVC ?


Char Captcha Example


After Captcha validation


Math Captcha Example

What is Captcha ?

Captcha is the validation layer to recognize the type of user before sending the data to server.

What are the advantages of Captcha ?

There are so many tool which is used to send automated message. If you have not implemented Captcha in your feedback or user registration page then you will get so many spam message or user.
So main advantage of Captcha to avoid the spam message or users.

How to use Captcha in MVC ?
There are so many open source library available to perform this task. Even though you can write your own code to implement Captcha in your application. This is totally depend on you.
In this article i m going to use CapchaMVC library from NuGet Packages. It is very simple to use in application.

Step 1: Create the Empty MVC application.

Step 2: Add the CapchaMVC library in Reference Layer like this.


Step 3: In Model layer add Feedback class and create the property like this

namespace MathCaptcha.Models
    public class Feedback
        public int Id { get; set; }
        public string Title { get; set; }
        public string Comment { get; set; }


Step 4: Create a HomeController and write the action method like this

using System.Web.Mvc;
using CaptchaMvc.HtmlHelpers;

namespace MathCaptcha.Controllers
    public class HomeController : Controller

        public ActionResult Index()
            return View();

        public ActionResult Index(string empty)
            // Code for validating the Captcha
            if (this.IsCaptchaValid("Captcha is not valid"))
                return RedirectToAction("ThankYouPage"); 

            ViewBag.ErrMessage = "Error: captcha is not valid.";
            return View();

        public ActionResult ThankYouPage() 
            return View();


Note: don’t forget to use CaptchaMvc.HtmlHelpers Namespace. In post action method i have written code for Captcha validation.

Step 5: Now create the Index View like this

@using CaptchaMvc.HtmlHelpers
@using MathCaptcha;
@using CaptchaMvc;

@model MathCaptcha.Models.Feedback

    ViewBag.Title = "Index";

@using (Html.BeginForm()) {


        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)

        <div class="editor-label">
            @Html.LabelFor(model => model.Comment)
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Comment,5,40,null)
            @Html.ValidationMessageFor(model => model.Comment)

         <br />
        <p class="Error">  @ViewBag.ErrMessage </p>
            <input type="submit" value="Send" />

Note: Here if you will use @Html.MathCaptcha() helper class then it will generate mathmatical capcha. If you will use @Html.Captcha(3) helper class then it will generate Char Capcha. 3 is the length of Captcha.

Step 6: Create the ThankYouPage index like this

@model MathCaptcha.Models.Feedback

    ViewBag.Title = "ThankYouPage";


<b> Thank you sending your valuable feedback to us.</b>

Point to remember

1. Just include CapchaMVC libray
2. Use the CaptchaMvc.HtmlHelpers namespace
3. Use MathCaptcha namespace for using mathCaptcha
4. Use CaptchaMvc namespace for using CharCapcha
5. For math Captcha use @Html.MathCaptcha() helper class
6. For Char Captcha use @Html.Captcha(3) helper class.


Here we have seen how to use Captcha in MVC application. It is very simple and straight forward to implement in MVC apllication.


4 thoughts on “How to implement Captcha in MVC ?

  1. jairam chakravarthi March 18, 2015 / 1:08 pm

    how can i generate complex captcha which is a mixture of caps and small and numbers

  2. Rakesh aingh April 20, 2017 / 1:02 pm

    i am doing the same thing but i am not getting captcha image.can u please tell me what could be the possible reasons for this…..and how can i get the captcha images.

    • Chandra Dev April 21, 2017 / 8:37 am

      Could you please reinstall the CapchaMvc from Nuget package? You might be missing the image. Please let me know if doesn’t work.

  3. Sajina N S August 16, 2021 / 11:08 am

    IsValidCaptcha method always returns false. can you please give me a solution

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.