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

Captcha MVC 2.3 is Released

Categories: News, Programming

Today, I released a new version of the Captcha MVC library, which supports the "intelligent" captcha. It is available at this link or via nuget. I wrote this article to introduce a new feature of this release.

What means "intelligent" captcha?

Every day we use the Internet and often meet with different captcha implementations, and we, as users do not like enter a security code. There are various techniques to make the captcha friendlier. For example, some developers create a “fake” input field and make it hidden for users and when a bot will tried to fill a form, it probably will fill this field.

How it works in CaptchaMvc?

The CaptchaMVC provides the IIntelligencePolicy interface which is responsible for creating an “intelligent” captcha. Here is it definition:
public interface IIntelligencePolicy
{
    bool? IsValid(ControllerBase controller, IParameterContainer parameterContainer);

    ICaptcha MakeIntelligent(ICaptcha captcha, IParameterContainer parameterContainer);
}
The CaptchaMVC provides two implementation of this interface:
  • FakeInputIntelligencePolicy – used a “fake” input to make an “intelligent” captcha, is what I wrote above.
  • JavaScriptIntelligencePolicy– used the JavaScript to make an “intelligent” captcha. Checks whether the user has JavaScript.
By default used the FakeInputIntelligencePolicy, but you can easily change this, using this code:
protected void Application_Start()
{
    //var captchaManager = (DefaultCaptchaManager) CaptchaUtils.CaptchaManager;

    //Default intelligence policy.
    //captchaManager.IntelligencePolicy = new FakeInputIntelligencePolicy(captchaManager);

    //JavaScript intelligence policy.
    //captchaManager.IntelligencePolicy = new JavaScriptIntelligencePolicy(captchaManager);
...
To make your captcha “intelligent”, you should use this extension method CaptchaMvc.HtmlHelpers.AsIntelligent(this ICaptcha, params ParameterModel[]).
This code shows how it works:
@Html.Captcha("Refresh", "Input", 5, "Is required field.", true).AsIntelligent()
@Html.MathCaptcha("Refresh", "Input", "Is required field.", true).AsIntelligent()
I have added some examples at this page.

Comments

Hi, this looks great. Would this work using only razor view engine and development? I see sample code for code behind .cs modules, do you have some for .cshtml?

Hi,
It supports razor and aspx engines.
What you mean by .cshtml?
You can use a partial view for your captcha like this:
Html.MathCaptcha("DefaultMathCaptchaMarkup", scriptPartialViewName: "DefaultMathCaptchaScript")
and then write .AsIntelligent() to make the captcha as "intelligent".
Or you can write your custom implementation of IIntelligencePolicy, and pass parameters to render your intelligence policy as you need.

Hi , I am looking for captcha without using cookies , why because i am using my webpage reg form in another application, in that time i have problem in IE ( in IE third party cookies are default is disable ), if you have any idea let me know once in mail, my mail id is ravadasantosh@gmail.com

Hi Vyachaslav,


Problem: I save in DB Captcha information, Tokeyn (Key) and Value. Now i check the Validation in Controller:


-------------------------------
[HttpPost, CaptchaVerify("Captcha is not valid")]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Controller( Model model)
{
if (ModelState.IsValid){}
}
-------------------------------
How can i get key from current captcha in my controller to detect this Captcha in my DB again.
_

как мне узнать какой КЕЙ в дании момент используется в контроллере для определения правильный ли Каптча был ведон?

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]