How to Implement CAPTCHA in ASP.Net MVC

CAPTCHAs


A CAPTCHA is a validation layer to recognize the type of user before sending data to the server.

Advantages of CAPTCHAs


There are many tools to send automated messages. If you have not implemented a CAPTCHA in your feedback or user registration page then you will get many spam messages or users.

So the main advantages of CAPTCHA is to avoid spam messages or users.

How to use CAPTCHA in ASP.NET MVC


There are many open-source libraries available to do this. Even though you can write your own code to implement a CAPTCHA in your application, that totally depends on you.

See the following images.

feedback form
                                                                  Char CAPTCHA Example

thank you page
                                                                  After CAPTCHA validation

After Captcha validation
                                                                     Math CAPTCHA Example

Step 1

Create the Empty ASP.NET MVC application.

Step 2

Add the CaptchaMvc library to the Reference Layer like this.

CapchaMVC

Step 3

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

  1. namespace MathCaptcha.Models  
  2. {  
  3.     public class Feedback  
  4.     {  
  5.         public int Id { getset; }  
  6.         public string Title { getset; }  
  7.         public string Comment { getset; }  
  8.   
  9.     }  
  10. }  

Step 4

Create a HomeController and write the action method like this.

  1. using System.Web.Mvc;  
  2. using CaptchaMvc.HtmlHelpers;  
  3.   
  4.   
  5. namespace MathCaptcha.Controllers  
  6. {  
  7.     public class HomeController : Controller  
  8.     {  
  9.   
  10.         public ActionResult Index()  
  11.         {  
  12.             return View();  
  13.         }  
  14.   
  15.         [HttpPost]  
  16.         public ActionResult Index(string empty)  
  17.         {  
  18.             // Code for validating the CAPTCHA  
  19.             if (this.IsCaptchaValid("Captcha is not valid"))  
  20.             {  
  21.                  
  22.                 return RedirectToAction("ThankYouPage");   
  23.             }  
  24.   
  25.             ViewBag.ErrMessage = "Error: captcha is not valid.";  
  26.             return View();  
  27.         }  
  28.   
  29.         public ActionResult ThankYouPage()   
  30.         {  
  31.             return View();  
  32.         }  
  33.   
  34.     }  
  35. }  

Note: Do not forget to use the CAPTCHA MVC.HtmlHelpers namespace. In the post action method I wrote code for CAPTCHA validation.

Step 5

Now create the Index View like this.

  1. @using CaptchaMvc.HtmlHelpers  
  2. @using MathCaptcha;  
  3. @using CaptchaMvc;  
  4.   
  5. @model MathCaptcha.Models.Feedback  
  6.   
  7. @{  
  8.     ViewBag.Title = "Index";  
  9. }  
  10.   
  11. @using (Html.BeginForm()) {  
  12.     @Html.ValidationSummary(true)  
  13.   
  14.     <fieldset>  
  15.         <legend>Feedback</legend>  
  16.   
  17.         <div class="editor-label">  
  18.             @Html.LabelFor(model => model.Title)  
  19.         </div>  
  20.         <div class="editor-field">  
  21.             @Html.EditorFor(model => model.Title)  
  22.             @Html.ValidationMessageFor(model => model.Title)  
  23.         </div>  
  24.   
  25.         <div class="editor-label">  
  26.             @Html.LabelFor(model => model.Comment)  
  27.         </div>  
  28.         <div class="editor-field">  
  29.             @Html.TextAreaFor(model => model.Comment,5,40,null)  
  30.             @Html.ValidationMessageFor(model => model.Comment)  
  31.         </div>  
  32.      
  33.         @Html.MathCaptcha()  
  34.   
  35.         @*@Html.Captcha(3)*@  
  36.          <br />  
  37.         <p class="Error">  @ViewBag.ErrMessage </p>  
  38.         <p>  
  39.             <input type="submit" value="Send" />  
  40.         </p>  
  41.     </fieldset>  
  42. }  
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.
  1. @model MathCaptcha.Models.Feedback  
  2.   
  3. @{  
  4.     ViewBag.Title = "ThankYouPage";  
  5. }  
  6.   
  7. <h2>ThankYouPage</h2>  
  8.   
  9. <b> Thank you sending your valuable feedback to us.</b>  
Points to remember 
  1. Just include the CaptchaMvc libray.

  2. Use the CaptchaMvc.HtmlHelpers namespace.

  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 to implement in MVC applications.


Similar Articles