Click here to Skip to main content
15,886,362 members
Articles / Web Development / HTML
Tip/Trick

Captcha Code Implementation in MVC

Rate me:
Please Sign up or sign in to vote.
3.03/5 (11 votes)
3 Jun 2014CPOL 48K   3.8K   7   12
Captcha code

Introduction

After lots of R&D and analysis of example of Captcha image generation and validation in MVC, finally I have decided to implement my custom Captcha code generation because all samples require lots of complex coding. But this is a very simple and quick way and requires less coding. This works fine for my requirement, maybe it will be useful for your needs.

Here is step by step code and description.

My model looks like:

C#
public class CaptchaModel
   {
       public string CapImage { get; set; }
       [Required(ErrorMessage = "Verification code is required.")]
       [System.ComponentModel.DataAnnotations.Compare("CapImageText",
           ErrorMessage = "Captcha code Invalid")]
       public string CaptchaCodeText { get; set; }
       public string CapImageText { get; set; }
   }

My controller looks like:

C#
public ActionResult Index()
      {
          CaptchaModel obj = new CaptchaModel();
          obj.CapImage = "data:image/png;base64," +
          Convert.ToBase64String(new CaptchaUtilityClass().VerificationTextGenerator());
          obj.CapImageText = Convert.ToString(Session["Captcha"]);
          return View(obj);
      }
      [HttpPost]
      public ActionResult Index(CaptchaModel objCap)
      {
          if (ModelState.IsValid)
          {
              return RedirectToAction("Thanks");
          }
          objCap.CapImage = "data:image/png;base64," +
          Convert.ToBase64String(new CaptchaUtilityClass().VerificationTextGenerator());
          return View("~/Views/Captcha/Index.cshtml", objCap);

      }
      public ActionResult Thanks()
      {
          return View();
      }

Finally, my view looks like:

C#
@model CaptchaImplementation.Models.CaptchaModel

@using (Html.BeginForm("Index", "Captcha", FormMethod.Post, new { Model }))
{
    @Html.ValidationSummary(false)
     <img src="@Model.CapImage"
     <label class="col-sm-5 control-label">Verification Code* :</label>
     @Html.TextBoxFor(t => t.CaptchaCodeText, new { @class = "form-control", 
     tabindex = "12", placeholder = "Verification Code" })
     @Html.HiddenFor(t => t.CapImageText)
     
    @*your action link or submit button*@                           
}

CaptchaUtilityClass.cs has image generation and random text generation code

Download the attachment.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) FuturismTechnologies
India India
from code project

Comments and Discussions

 
QuestionVery good job, but compare the typed value with the value in session Pin
tulioccalazans7-Nov-14 0:25
tulioccalazans7-Nov-14 0:25 
QuestionVery Good Article!! Pin
Rakesh Devare24-Jun-14 1:34
Rakesh Devare24-Jun-14 1:34 
QuestionVery nice article Pin
amarjeet001123-Jun-14 21:45
amarjeet001123-Jun-14 21:45 
AnswerRe: Very nice article Pin
jayant jaiswal23-Jun-14 21:50
jayant jaiswal23-Jun-14 21:50 
QuestionNice Article Pin
roshankuchankar23-Jun-14 21:44
roshankuchankar23-Jun-14 21:44 
QuestionNice article.. Pin
Sharda Jaiswal6-Jun-14 18:51
Sharda Jaiswal6-Jun-14 18:51 
GeneralExcellent article Pin
snehal.botre6-Jun-14 1:40
snehal.botre6-Jun-14 1:40 
GeneralPerfect! Pin
ashbind's4-Jun-14 3:14
ashbind's4-Jun-14 3:14 
QuestionNice Article! Pin
greatonkar4-Jun-14 3:00
greatonkar4-Jun-14 3:00 
GeneralExcellent Article Pin
vinod Kumar koshta4-Jun-14 2:43
vinod Kumar koshta4-Jun-14 2:43 
GeneralNice Article !!! Pin
Sagar V4-Jun-14 2:39
Sagar V4-Jun-14 2:39 
GeneralMy vote of 3 Pin
Sunasara Imdadhusen4-Jun-14 1:29
professionalSunasara Imdadhusen4-Jun-14 1:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.