Getting started

  • Download latest version of CaptchaMvc from site or nuget (CaptchaMvc.Mvc3 or CaptchaMvc.Mvc4).
  • Add the reference to CaptchaMvc.dll in your project.
  • Include the namespace (@using CaptchaMvc.HtmlHelpers) of the extension method for the views.

Once you do that you can use helper methods to create the captcha. By default there are two types of captcha, plain and mathematical.

To create a plain captcha, there are three methods:

  • Html.Captcha(int) - Takes only the length of the characters. The input field is not required. Text input characters will be equal to 'Input symbols' and the button text to update will be equal to 'Refresh'.
  • Html.Captcha(string, string, int) - The input field is not required. Text input characters will be equal to the first argument and the button text to update will be equal to the second argument. The length of characters will be equal to the third argument.
  • Html.Captcha(string, string, int, string) - The input field is required. Text input characters will be equal to the first argument and the button text to update will be equal to the second argument. The length of characters will be equal to the third argument. The fourth argument accepts error messages for required field.
  • Html.Captcha(int, string) - The length of characters will be equal to the first argument. The second argument accepts a partial view name to render the captcha.
  • Html.Captcha(int, string, ViewDataDictionary) - The length of characters will be equal to the first argument. The second argument accepts a partial view name to render the captcha. The third argument accepts the view data dictionary for the partial view.
As a result, you get something like this:
PlainCaptcha.png

To create a mathematical captcha, there are three methods:

  • Html.MathCaptcha() - The input field is not required. Text input characters will be equal to 'The answer is' and the button text to update will be equal to 'Refresh'.
  • Html.MathCaptcha(string, string) - The input field is not required. Text input characters will be equal to the first argument and the button text to update will be equal to the second argument.
  • Html.MathCaptcha(string, string, string) - The input field is required. Text input characters will be equal to the first argument and the button text to update will be equal to the second argument. The third argument accepts error messages for required field error.
  • Html.MathCaptcha(string) - The first argument accepts a partial view name to render the captcha.
  • Html.MathCaptcha(string, ViewDataDictionary) - The first argument accepts a partial view name to render the captcha. The second argument accepts the view data dictionary for the partial view.
As a result, you get something like this:
MathCaptcha.png

How to check the captcha?

To verify the captcha, you can use an attribute(CaptchaMvc.Attributes.CaptchaVerify) or extension methods for a ControllerBase.

An example of an attribute usage:
        [CaptchaMvc.Attributes.CaptchaVerify("Captcha is not valid")]
        [HttpPost]
        public ActionResult MathCaptcha(string empty)
        {
            if (ModelState.IsValid)
            {
                TempData["Message"] = "Message: captcha is valid.";
                return View();
            }

            TempData["ErrorMessage"] = "Error: captcha is not valid.";
            return View();
        }

An example of an extension methods for a ControllerBase usage:
        [HttpPost]
        public ActionResult Index(string empty)
        {
            if (this.IsCaptchaVerify("Captcha is not valid"))
            {
                TempData["Message"] = "Message: captcha is valid.";
                return View();
            }

            TempData["ErrorMessage"] = "Error: captcha is not valid.";
            return View();
        }

Last edited Jul 23, 2012 at 1:03 PM by VVS0205, version 7

Comments

puredevelopers Dec 13, 2013 at 9:11 PM 
I just added this widget to my project, and it renders the captcha just fine, but adding the attribute to my controller does nothing, and the controller doesn't recognize this.IsCaptchaVerify. It throws an exception. Am I missing something?

ChrisRandle Jul 10, 2013 at 11:44 PM 
I get nothing. Installed via Nuget. Created new document with jQuery added as a script, added the @Html.Captcha(4) reference and although it rendered the HTML, the src attribute of the image was empty. Am going to use something else. This is really frustrating and cost me the last 3 hours of my life trying to get working. Grr!

Blazkovicz Mar 15, 2013 at 7:06 AM 
Hello, I decided to use this project with own changes, so I downloaded it and decompiled.

Code that I saw inside disapointed me in CodePlex.com, such ugly code they provide to download.

On the outside you see tiny helper methods, but what each of these methods does actually?
You type Html.Captcha(int) and expect it to insert code to your view, but before that it does unnecessary spade-work each time:

1) method wraps each input argument into struct and calls next method
2) next method wraps reported structs into container (smth similar to dictionary, but own), calls next method
3) next method unwraps(!) all arguments from container into agruments of originally form and wraps(!) into new struct, calls next method
4) next method decides what type of handler to use for reported struct, creates handler and calls its method (why no do the work already?)
5) handlers method really does work, calling huge amount of external methods, defined in other classes of project.

Plus to all this, there are virtual methods, that never will be overriden, never used variables, public methods, that should be private, etc..

Plus to all this, method generates really weak captcha, using just to types of font and one color.

Nothing personal. I will use this code as the start of my own (already threw away models, builders, manager and utils). I will add more fonts, colors, gradient background and image rotations/deformations. One thing in code was useful to learn: jetbrains annotaions attributes, thanks!

VVS0205 Jan 22, 2013 at 2:47 PM 
Thank you for your feedback.
On the first item, in new version I will add this feature.
On the second item, you can use .resx files to localize captcha, validation also support .resx files.
On the third item, I will try do something with this.

Regards,
Vyacheslav Volkov

gauravrajput Nov 18, 2012 at 4:27 PM 
Hi,

This is nice CAPTCHA and easy to use in MVC . I have few suggestions request you to please incorporate the first one atleast.

Firstly Your mathematical calculations should be single digits. Its quite painful many times to calculate 3 digits and two digits

Secondly, No support for Localization in your text CAPTCHA?

Thirdly, it makes the page load little slower


Regards

Gaurav