About .NET, ASP.NET, MVC, C#, WPF, WCF and everything related to .NET and more.

Captcha Mvc

Captcha Mvc - is a library written in C#, that you can use in your MVC project.
The project page on the codeplex.
Source code is available on the link.

Features:

  • You can easily change or extend the current implementation of the captcha.
  • There's two types of captcha plain and mathematical.
  • Supports for storing captcha in the session or cookie, but you can easily add your implementation.
  • Supports MVC 3-4.
  • Supports the "intelligent captcha".

Comments

HI
I very like your lib
I have a question!!
How can I Custom Captcha image size??

Hi Scott,
Here's an example of how you can set the default size:

protected void Application_Start()
{
    CaptchaUtils.ImageGenerator.Height = your size;
    CaptchaUtils.ImageGenerator.Width = your size;

    ...

HI, this seems to be good solution , but i would like to use captchamvc within viewModel so my question is how to put it in model and then veryfy it

The HTML generated by helper is not valid HTML. Is it possible to fix it in next release?

Hi Vyacheslav,

I am just got "CaptchaMvc.Mvc3.1.5.0" but I am having trouble in getting the Captcha to work :

My view contains :
@CaptchaMvc.HtmlHelpers.CaptchaHelper.Captcha(this.Html, 6)

and trying the submit my form in a JQuery.ajax() Method as shown below :
$.ajax({ type: "Post",
url: '<someURL>',
data: { A : name , B: age, C: gender}
});

Within the controller when I perform this , I always get 'false' :

[HttpPost]
[CaptchaMvc.Attributes.CaptchaVerify("Captcha Problem")]
public JsonResult Submit (Person model)
{
if (CaptchaMvc.HtmlHelpers.CaptchaHelper.IsCaptchaValid(this, "Captcha Error"))
{
//Some C# code
}
// The IsCaptchaValid() Method always returns false
}

Please help and please let me know what am I missing here . Thanks in advance for your help.

Anand

Hi,

I found the fix to this behaviour.

Let's say that you have this controller method:

[HttpPost, CaptchaVerify("Captcha is not valid")]
public ActionResult PartialCaptchaView(string empty)
{
//...your code...
if (this.IsCaptchaValid("Captcha is not valid")){
//more code..
}
//more code...
}

to make method "IsCatpchaValid" work you just have to remove "CaptchaVerify" inside the brackets.
If you want to use "ModelState.IsValid" property leave "CaptchaVerify" inside the brackets.

I hope this helps.

I have a same problem too, CaptchaMvc.HtmlHelpers.CaptchaHelper.IsCaptchaValid(...) always return false. Please help!

Put this line in your Get Method

CaptchaUtils.CaptchaManager.StorageProvider = new CookieStorageProvider();

Hi,

I found the fix to this behaviour.

Let's say that you have this controller method:

[HttpPost, CaptchaVerify("Captcha is not valid")]
public ActionResult PartialCaptchaView(string empty)
{
//...your code...
if (this.IsCaptchaValid("Captcha is not valid")){
//more code..
}
//more code...
}

to make method "IsCatpchaValid" work you just have to remove "CaptchaVerify" inside the brackets.
If you want to use "ModelState.IsValid" property leave "CaptchaVerify" inside the brackets.

I hope this helps.

Hi!

How can I use it with MVC but using ASP sintaxis?

Thanks in advance!

I would be interested in that answer too!

Hi there,
Add this code to your .aspx page, and it should work:

<%@ Import Namespace="CaptchaMvc.HtmlHelpers" %>
<%--        your markup here--%>
 <%: Html.MathCaptcha() %>

I have this following code in Razor view

Hi,

I have a question related to Captcha MVC.
How can we use 2 captcha in 2 different forms on the same page with a refresh function because the refresh post update always the same img.

Thanks in advance!

Stefanie

Hi Stefanie,
Here is a link to an example of multiple captcha.

Hi all,

I thought I would just help some of you out with the pure ajax issues.

In short when you use a beginform all the captcha is actually doing in the backend is sending an ajax using the beginform with the CaptchaDeText and CaptchaInputText fields. These two fields in a post request are how the captchaMVC process deals with being able to check the validity of the captcha codes entered.

If these two values are not being sent to the controller that is using the this.IsCaptchaValid, then it will always return false.

for example from "Anand"'s comment above:

$.ajax({ type: "Post",
url: '<someURL>',
data: { A : name , B: age, C: gender}
});

even though in your controller function you probably only have "string A, int B, string C" you should still be doing:-

$.ajax({ type: "Post",
url: '<someURL>',
data: { CaptchaDeText: $("#CaptchaDeText").val(), CaptchaInputText: $("#CaptchaInputText").val() , A : name , B: age, C: gender}
});

Hi Vyacheslav,

THANK YOU for your wonderfull work with Captcha MVC.

I have only one Problem that I cannot see and use the "refresh" link

I use the HTML exactly like in your examples (your example app is working by the way).

And it happens with all ParameterKeys 0 to 2

Any ideas why this could be??? (I use MVC 5 with all updates since today).

THANK YOU for your help. cU Andreas



Hi Andreas,
Maybe you haven't added JQuery script, try to declare jQuery script in your layout page and put it in the head tag.

hi Vyacheslav
i work in mvc ajax. i dont use this captcha becouse it is always return false.
how can i use it?
i write these:
@Html.Captcha("1")
<button id="btn" class="btn btn-danger" type="submit">Save</button>


---------------------------------------------------------
$('#btn').click(function (e) {
e.preventDefault();
var url = 'Home/Save';

$.ajax({
url: url,
type: "POST",
data: formData,
processData: false,
contentType: false,
});
});
----------------------------------------------------
[HttpPost, CaptchaVerify("Captcha is not valid")]
public ActionResult Save()
{
if (ModelState.IsValid)
return Json(false, JsonRequestBehavior.AllowGet);
else
return Json(true, JsonRequestBehavior.AllowGet);

}

David 's problem is my issue too

Hi Vyacheslav,
Thank you very much for the excellent work.

I was able to use the captcha without any problem with full page/form submit, following your sample.

If i do the captcha validation by posting the captcha params along with my form data as mentioned by David above, but thru jQuery.post(...) , for the first time the captcha is matched on the server but for some reason if my view Model validation failed i send Json(..) response back to view with those modal validation errors.

In that instance, if i click on 'Refresh' link, then the 'Refresh' link disappears and also captcha is not refreshed. So is there any data/config that i need to send back to view along with my modal validations Json?

Siva,

I have it working in a true ajax way and understand what is going on now, even with the refresh issue after success. I will either reply in this thread or put together a base project with a working example and provide the link with a tutorial on my site.

Vyacheslav,
As a fellow developer, I am very disappointed that you seem to deliberately ignore the repeated issue of the ajax post captcha validation in the controller issue. Part of responsibility of releasing software, tools, and apis is also supporting them. I have seen several inquiries from frustrated developers asking why it is not working and to provide a realistic and working example. None of your code in your project examples utilize the IsCaptchaValid method.

Can you please provide a working and proven example of this and/or officially state why it is not working and what needs to be done to fix it? I understand David's example and why that would be part of the issue, but it seems that others are still having problems with it? Is what David said the issue? Can you reply and provide a simple code snippet of an ajax post with form data and any other values for the captcha, a controller with parameters needed, and then a conditional statement that has been tested by yourself to show that IsCaptchaValid works in that example?

Hi there,
Here's full path for ajax form sample in the GettingStarted project: MvcCaptcha.Examples.Mvc4\GettingStarted\Views\Home\AjaxForm2.cshtml.
I also added new ajax form example.
I hope this helps.

Vyacheslav,

I appreciate the new ajax form example and promptness of response. Although it is not ideal and true ajax process due to calling location.reload(); in your success on checking IsOK in your ajax call on the view. It did help me, however, get to the right way. I know the reason you do this is to refresh the captcha parameters. But I do not think this is the best solution. For instance, imagine if I had a partial view contact page and that was loaded via ajax upon clicking a link on my site. Calling would location.reload(); on success of submitting the contact form data would display for the viewer the default loaded content of the page, which in this case may not be the contact form page, which is only loaded upon clicking on the link via ajax. I hope this makes sense. I will go ahead and either provide an example here or link to one with a fully ajax example.

Vyacheslav,

I appreciate the new ajax form example and promptness of response. Although it is not ideal and true ajax process due to calling location.reload(); in your success on checking IsOK in your ajax call on the view. It did help me, however, get to the right way. I know the reason you do this is to refresh the captcha parameters. But I do not think this is the best solution. For instance, imagine if I had a partial view contact page and that was loaded via ajax upon clicking a link on my site. Calling would location.reload(); on success of submitting the contact form data would display for the viewer the default loaded content of the page, which in this case may not be the contact form page, which is only loaded upon clicking on the link via ajax. I hope this makes sense. I will go ahead and either provide an example here or link to one with a fully ajax example.

And also thank you for providing a great plugin. I understand it is difficult supporting something, especially for years now, as well as overcoming any language barriers. So, thank you greatly for taking the time to respond and provide examples. It is truly appreciated and I look forward to keeping up with your latest works.

I want to make some changes in textbox where value is entered. And also want to remove the "input symbols" tag.

why don't extend the audio support for cling person?

Hi,


Can I have one digit Math Captcha? Two digits gives hard ones, like 86 - 42!

Thanks in advance,

Hi,

I am using the math captcha for mvc5. Whenever I try to use it on a mobile device, it returns "captcha is not valid"

Do you know what could cause the problem of captcha image not showing up on MVC5? I mean, the img tag has "src(unknown)". It seems that is not able to read the Generate method from assembly.

Leave a Reply
*bold*
_italics_
+underline+
* Bullet List
** Bullet List 2
# Number List
## Number List 2
{"Do not apply formatting"}
{code:language} code here {code:language}.
Supports: aspx c#, c#, c++, html, sql, xml
[url:http://www.example.com]