Click here to Skip to main content
13,344,016 members (82,859 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 3 Jun 2014

Captcha Code Implementation in MVC

, 3 Jun 2014
Rate this:
Please Sign up or sign in to vote.
Captcha code


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:

public class CaptchaModel
       public string CapImage { get; set; }
       [Required(ErrorMessage = "Verification code is required.")]
           ErrorMessage = "Captcha code Invalid")]
       public string CaptchaCodeText { get; set; }
       public string CapImageText { get; set; }

My controller looks like:

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);
      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:

@model CaptchaImplementation.Models.CaptchaModel

@using (Html.BeginForm("Index", "Captcha", FormMethod.Post, new { Model }))
     <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.


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


About the Author

jayant jaiswal
Software Developer (Senior) FuturismTechnologies
India India
from code project

You may also be interested in...


Comments and Discussions

QuestionVery good job, but compare the typed value with the value in session Pin
tulioccalazans7-Nov-14 1:25
membertulioccalazans7-Nov-14 1:25 
QuestionVery Good Article!! Pin
Rakesh Devare24-Jun-14 2:34
memberRakesh Devare24-Jun-14 2:34 
QuestionVery nice article Pin
amarjeet001123-Jun-14 22:45
memberamarjeet001123-Jun-14 22:45 
AnswerRe: Very nice article Pin
jayant jaiswal23-Jun-14 22:50
memberjayant jaiswal23-Jun-14 22:50 
QuestionNice Article Pin
roshankuchankar23-Jun-14 22:44
memberroshankuchankar23-Jun-14 22:44 
QuestionNice article.. Pin
Sharda Jaiswal6-Jun-14 19:51
memberSharda Jaiswal6-Jun-14 19:51 
GeneralExcellent article Pin
snehal.botre6-Jun-14 2:40
membersnehal.botre6-Jun-14 2:40 
GeneralPerfect! Pin
ashbind's4-Jun-14 4:14
memberashbind's4-Jun-14 4:14 
QuestionNice Article! Pin
greatonkar4-Jun-14 4:00
membergreatonkar4-Jun-14 4:00 
GeneralExcellent Article Pin
vinod Kumar koshta4-Jun-14 3:43
membervinod Kumar koshta4-Jun-14 3:43 
GeneralNice Article !!! Pin
Member 79863334-Jun-14 3:39
memberMember 79863334-Jun-14 3:39 
GeneralMy vote of 3 Pin
Sunasara Imdadhusen4-Jun-14 2:29
professionalSunasara Imdadhusen4-Jun-14 2: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 4 Jun 2014
Article Copyright 2014 by jayant jaiswal
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid