---
title: "Implement Captcha in ASP.NET MVC4"  
description: "Hi everyone in this article, I’m explaining about Captcha Code in Asp.Net MVC4"  
author: "Anonymous User"  
published: 2014-11-26  
updated: 2020-03-06  
canonical: https://www.mindstick.com/articles/1540/implement-captcha-in-asp-dot-net-mvc4  
category: "asp.net"  
tags: ["security in .net", "asp.net mvc", "validation"]  
reading_time: 5 minutes  

---

# Implement Captcha in ASP.NET MVC4

Hi everyone in this article, I’m explaining about Captcha Code in Asp.Net MVC4\

##### Description:

A CAPTCHA is a [validation](https://www.mindstick.com/articles/43/validation-controls-in-asp-dot-net) layer to recognize the type of user before sending data to the server.

##### Advantage of Captcha Code

There are many tools to send automated messages. If you have not implemented a CAPTCHA in your feedback or user [registration](https://answers.mindstick.com/qa/116207/what-is-lei-registration) page then you will get many spam messages or users.

So the main [advantages](https://www.mindstick.com/articles/44063/the-individual-advantages-of-short-online-courses) of CAPTCHA is to avoid spam messages or users.

##### How to use Captcha in ASP.Net MVC4

There are many open-soure libraries available to do this. Even though you can write your own code to implement a CAPTCHA in your [application](https://www.mindstick.com/articles/12824/calculator-application-in-android), that totally depends on you.

##### Step 1

Create the new [Asp.Net Mvc](https://www.mindstick.com/articles/1571/start-with-asp-dot-net-mvc-4) Application

Open [visual Studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) >> File >> New >> Project

**![Implement Captcha in ASP.NET MVC4](https://www.mindstick.com/mindstickarticle/0258ace3-910a-480a-b8ff-3f450124f829/images/287c6d23-da05-4a77-b87a-39d20501b588.png)**

##### Give the Project name “Captcha”

**![Implement Captcha in ASP.NET MVC4](https://www.mindstick.com/mindstickarticle/0258ace3-910a-480a-b8ff-3f450124f829/images/1e0b8287-4b11-4a73-8886-7bc9297dcf52.png)**

##### Step 2

Add the CaptchaMvc library to the reference layer like this.

![Implement Captcha in ASP.NET MVC4](https://www.mindstick.com/mindstickarticle/0258ace3-910a-480a-b8ff-3f450124f829/images/5e197e63-ad14-4ba0-96d7-e1b5f3693c91.png)

##### Step 3

In the model layer add a Feedback class and create the property like this.

```
using System;using System.Collections.Generic;using System.Linq;using System.Web; namespace Captcha.Models{    public class Feedback    {        public int Id { get; set;
    }        public string Title { get; set;
    }        public string Comment { get; set;
    }       }}
```

##### Step 4

Create HomeController and write the action method like this.

```
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using CaptchaMvc.HtmlHelpers;

namespace Captcha.Controllers{
    public class HomeController : Controller    {
        //
        // GET: /Home/

        public ActionResult Index()        {
            return View();
        }
        [HttpPost]        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: Do not forget to use the using CaptchaMvc.HtmlHelpers; in the post action method I wrote code for captcha validation

##### Step 5

Now create the index view like this.

```
@using CaptchaMvc.HtmlHelpers@using Captcha;@using CaptchaMvc;
@model Captcha.Models.Feedback
@{
    ViewBag.Title = "Index";
}

<style>    .editor-label {        width: 20%;        float: left;
    }
    .editor-field {        width: 76%;        float: left;
    }
    input[type='text'] {        width: 100%;        height: 35px;
    }
    textarea {        width: 100%;        height: 100px;
    }
</style>

@using (Html.BeginForm()){
    @Html.ValidationSummary(true)     <br />    <br />    <fieldset style="width: 50%; margin: 0 auto;">        <legend>Register</legend>        <div>
            <div class="editor-label">                @Html.LabelFor(model => model.Title)            </div>
            <div class="editor-field">                @Html.EditorFor(model => model.Title)                @Html.ValidationMessageFor(model => model.Title)
            </div>            <br />            <br />            <br />        </div>
        <div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Comment)
            </div>
            <div class="editor-field">
                @Html.TextAreaFor(model => model.Comment, 5, 40, null)
                @Html.ValidationMessageFor(model => model.Comment)
            </div>
            <br /><br /><br /><br /><br /><br />
        </div>

        @Html.MathCaptcha()
        @*@Html.Captcha(3)*@
        <br />
        <p class="Error">@ViewBag.ErrMessage </p>
        <p style="text-align:right;padding-right:20px;">
            <input type="submit" value="Send" />
        </p>
    </fieldset>
}  
```

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

##### Step 6

Create the ThankYouPage index like this.

```
@model
Captcha.Models.Feedback @{      ViewBag.Title = "ThankYouPage";  } <h2>ThankYouPage</h2> <b>Thank you sending your
valuable feedback to us.</b> 
```

**Output**

**![Implement Captcha in ASP.NET MVC4](https://www.mindstick.com/mindstickarticle/0258ace3-910a-480a-b8ff-3f450124f829/images/8bc74b15-a3e2-4e1f-b3a6-870c9535a700.png)**

##### Insert right Captcha Code

**![Implement Captcha in ASP.NET MVC4](https://www.mindstick.com/mindstickarticle/0258ace3-910a-480a-b8ff-3f450124f829/images/a66ccad6-e2fe-42db-9d11-c6aae639b816.png)**

##### After click Send Output

**![Implement Captcha in ASP.NET MVC4](https://www.mindstick.com/mindstickarticle/0258ace3-910a-480a-b8ff-3f450124f829/images/00d94b91-d4b9-4b62-aa45-d8d5feeafd97.png)**

##### Points to remember

#####

1. Just include the CaptchaMvc libray.
2. Use the CaptchaMvc.HtmlHelpers [namespace](https://www.mindstick.com/articles/28/namespace-in-dot-net).
3. Use the MathCaptcha namespace for using mathCaptcha.
4. Use the CaptchaMvc namespace for using CharCaptcha.
5. For a math CAPTCHA use the @Html.MathCaptcha() helper class.
6. For a Char CAPTCHA use the @Html.Captcha(3) helper class.

##### Summary

Here we saw how to use a CAPTCHA in an ASP.NET MVC application. It is very simple and [straight forward](https://answers.mindstick.com/qa/31224/which-is-the-most-straight-forward-approach-for-planning-algorithm) to implement in MVC applications.

In my next post I’ll explain about Check Availability of user name or email using ASP.NET, Ajax and Jquery**\**

---

Original Source: https://www.mindstick.com/articles/1540/implement-captcha-in-asp-dot-net-mvc4

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
