Captcha Mvc [ 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 extension methods to create a captcha. By default, there are two types of captcha, plain and mathematical.

Methods to create a plain captcha:

Html.Captcha(int, params ParameterModel[]) - takes only the length of the characters. The input field is not required. Text of input will be equal to 'Input symbols' and the text of update button will be equal to 'Refresh'.

Html.Captcha(string, string, int, params ParameterModel[]) - the input field is not required, text of input will be equal to second parameter, and the text of update button will be equal to first parameter. The length of characters will be equal to the third argument.

Html.Captcha(string, string, int, string, params ParameterModel[]) - the input field is required and error message is equals to the fourth parameter, text of input will be equal to second parameter, and the text of update button will be equal to first parameter. The length of characters will be equal to the third parameter.

Html.Captcha(int, string, ViewDataDictionary = null, params ParameterModel[]) - the length of characters will be equal to the first parameter. Second parameter takes a partial-view to render captcha. Third parameter takes a ViewDataDictionary for the partial-view.

Html.Captcha(int, string, string, ViewDataDictionary = null, params ParameterModel[]) - the length of characters will be equal to the first parameter. Second parameter takes a partial-view to render captcha. Third parameter takes a partial-view to render scripts of captcha. Fourth parameter takes a ViewDataDictionary for the partial-view.

Methods to create a mathematical captcha:

Html.MathCaptcha(params ParameterModel[]) - the input field is not required. Text of input will be equal to 'The answer is' and the text of update button will be equal to 'Refresh'.

Html.MathCaptcha(string, string, params ParameterModel[]) - the input field is not required, text of input will be equal to second parameter, and the text of update button will be equal to first parameter.

Html.MathCaptcha(string, string, string, params ParameterModel[]) - the input field is required and error message is equals to the fourth parameter, text of input will be equal to second parameter, and the text of update button will be equal to first parameter.

Html.MathCaptcha(string, ViewDataDictionary = null, params ParameterModel[]) - first parameter takes a partial-view to render captcha. Second parameter takes a ViewDataDictionary for the partial-view.

Html.MathCaptcha(string, string, ViewDataDictionary = null, params ParameterModel[]) - first parameter takes a partial-view to render captcha. Second parameter takes a partial-view to render scripts of captcha. Third parameter takes a ViewDataDictionary for the partial-view.

* As you can see, all methods take an array of ParameterModel, its optional parameter, but if you want to change default behavior this parameter may be useful to you. In further examples we will be use it.

How to check the captcha?

There are two ways to do this.Example of usage attribute to check the captcha:

Is it possible to wrap JQuery call which starts Captcha into $(function(){})?In this case i don't need to reference JQuery in the header. My Layout references JQuery at the very bottom of the page, and i want to keep it like this.

Hi,Captcha already uses this construction $(function(){}), but for this code is required to define JQuery above it, otherwise you will get an error $ is not defined. To avoid this error you can split the captcha markup and script generation.

Splitting up the CAPTCHA from the script worked fine, however it becomes a nuisance in some scenarios. e.g. I want to use the CAPTCHA in a partial, and jQuery is loaded at the end of my body in a _Layout.cshtml file.

My controller is using the Attribute method to check against the page. It all works great except when there is a wrong answer. Nothing shows. Do I need to place the TempData object somewhere on the page or is it suppose to be built-in to the Captcha?

Hi,Which version of CaptachMvc are you using?Here's full path to this method CaptchaMvc.HtmlHelpers.CaptchaHelper.IsCaptchaValid, before this method is called CaptchaMvc.HtmlHelpers.CaptchaHelper.IsCaptchaVerify.If you are using an older version I recommend the upgrade to the latest.

Hi there,If you use the SessionStorageProvider (by default), it depends on the user's session if the session is invalidated, the values ​​in the captcha will be deleted. You can change the storage provider to the CookieStorageProvider using this code:

I am getting a null reference exception when I try and render the captcha. Oddly this works on my collegues machines. Is there anything in IIS that needs set up? I am using MVC 5 (although oddly the error message has MVC3 in it (assuming this is just the path the code is held at)

[NullReferenceException: Object reference not set to an instance of an object.] CaptchaMvc.Infrastructure.CaptchaUtils.GetFromSession(String key, Func`1 getItem) in g:\CodePlex\Captcha\CaptchaMvc(Mvc 3)\Infrastructure\CaptchaUtils.cs:224 CaptchaMvc.Infrastructure.SessionStorageProvider.get_DrawingKeys() in g:\CodePlex\Captcha\CaptchaMvc(Mvc 3)\Infrastructure\SessionStorageProvider.cs:144 CaptchaMvc.Infrastructure.SessionStorageProvider.Add(KeyValuePair`2 captchaPair) in g:\CodePlex\Captcha\CaptchaMvc(Mvc 3)\Infrastructure\SessionStorageProvider.cs:52

Hi,I want to display captcha on login page if user tipe a wrong password.At controller method, if i decorate with [CaptchaMvc.Attributes.CaptchaVerify("Captcha is not valid")] and at view i have an if statement that display or not the captcha.Method from controller return all the time "Captcha is not valid" because ModelState contains the Captcha even if I haven't display it.How can I display the error message only if captcha is displayed on view and is typed wrong?Help me please!Thanks

Captcha seems to be not working if referrer information is not enabled in the Browser.If I disable referrer in the browser then I only get the error bitmap (black background with red cross), generated by GetErrorBitmap. Is there a way to make it work without referrer?

Hi,I am using IsCaptchaValid Extension method for validating the plain captcha. It works fine in local and QA environments. But it fails for the first time in the Production and works fine on second attempt. Can someone help giving the suggestion?

I use jquery and ajax to check the captcha, it validates correctly in the controller, but such issue exists :

1. On the first load, the refresh button work correctly, and also the validation (it checks the entered captcha correctly).2. But when I entered the incorrect captcha text, the correct error displayed, and after that the refresh button don't work again, and also the validation (I enter the correct captcha but it's stated as invalid).

as note, I return a json object in the controller when validating the captcha. I'm not sure if that's affecting.

I also tried the "split markup and script generation" but it doesn't fix the issue.