Click here to Skip to main content
Click here to Skip to main content

Captcha Code Implementation in MVC

, 3 Jun 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

 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:

  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:

@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)

Share

About the Author

jayant jaiswal
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 [modified] Pinmembertulioccalazans7-Nov-14 1:25 
QuestionVery Good Article!! PinmemberRakesh Devare24-Jun-14 2:34 
QuestionVery nice article Pinmemberamarjeet001123-Jun-14 22:45 
AnswerRe: Very nice article Pinmemberjayant jaiswal23-Jun-14 22:50 
QuestionNice Article Pinmemberroshankuchankar23-Jun-14 22:44 
QuestionNice article.. PinmemberSharda Jaiswal6-Jun-14 19:51 
GeneralExcellent article Pinmembersnehal.botre6-Jun-14 2:40 
GeneralPerfect! Pinmemberashbind's4-Jun-14 4:14 
QuestionNice Article! Pinmembergreatonkar4-Jun-14 4:00 
GeneralExcellent Article Pinmembervinod Kumar koshta4-Jun-14 3:43 
GeneralNice Article !!! PinmemberMember 79863334-Jun-14 3:39 
GeneralMy vote of 3 PinprofessionalSunasara Imdadhusen4-Jun-14 2:29 

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

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

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