MVC Captcha using dll

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Getting started

  • Download the latest version of CaptchaMvc from the 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.
1
<strong>NOTE:Get the example files from <a href="https://codeplexarchive.blob.core.windows.net/archive/projects/captchamvc/captchamvc.zip">captchamvc.zip</a></strong>
<strong>NOTE:Get the example files from <a href="https://codeplexarchive.blob.core.windows.net/archive/projects/captchamvc/captchamvc.zip">captchamvc.zip</a></strong>

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 the 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 errors.
  • 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

For Numeric captcha only add key in web.config of your project

<add key =”CaptchaChars” value=”0123456789″/>

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 attribute usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
        [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();
        }
        [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 ControllerBase usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
        [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();
        }
 
<strong>NOTE:</strong>Get the example files from <a href="https://codeplexarchive.blob.core.windows.net/archive/projects/captchamvc/captchamvc.zip"><strong>captchamvc.zip</strong></a>
        [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();
        }

<strong>NOTE:</strong>Get the example files from <a href="https://codeplexarchive.blob.core.windows.net/archive/projects/captchamvc/captchamvc.zip"><strong>captchamvc.zip</strong></a>

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

2 thoughts on “MVC Captcha using dll

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.