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

Captcha MVC 2.4 is Released

Categories: Programming

Today, I released a new version of the Captcha MVC library, it is available at this link or via nuget.

What's new:

  • Changed the IIntelligencePolicy interface, added ICaptchaManager as parameter for all methods.
  • Improved font size in the image, thanks abot user.
  • Added the ResponseTimeIntelligencePolicy class - is the time-dependent validation for captcha, thanks reinhardS user for the idea.
  • Added the MultiIntelligencePolicy class that allows the use of multiple IIntelligencePolicy.
  • Added the StorageType enum is used by the IIntelligencePolicy.
  • Fixed problem with datacontract serialization.
  • Added the AddAreaRouteValue property to the DefaultCaptchaManager class.
  • Removed obsolete methods.
  • Minor changes.
Below I will describe the changes in more detail.

Changed the IIntelligencePolicy interface, added ICaptchaManager as parameter for all methods.

In version 2.3:
public interface IIntelligencePolicy
{
   bool? IsValid(ControllerBase controller, IParameterContainer parameterContainer);

   ICaptcha MakeIntelligent(ICaptcha captcha, IParameterContainer parameterContainer);
}
In version 2.4:
public interface IIntelligencePolicy
{
   bool? IsValid(ICaptchaManager captchaManager, ControllerBase controller,
 IParameterContainer parameterContainer);

   ICaptcha MakeIntelligent(ICaptchaManager captchaManager, ICaptcha captcha, 
IParameterContainer parameterContainer);
}

Added the ResponseTimeIntelligencePolicy class

This class allows you to set the time during which the captcha will not be considered valid.
Consider an example:
//Getting the current ICaptchaManager
ICaptchaManager captchaManager = CaptchaUtils.CaptchaManager;

//After this time the captcha will be considered valid.
TimeSpan time = TimeSpan.FromSeconds(4);
captchaManager.IntelligencePolicy = new ResponseTimeIntelligencePolicy(time);

Added the MultiIntelligencePolicy

This class allows you to combine different IIntelligencePolicy into one.
Consider an example:
//Getting the current ICaptchaManager
ICaptchaManager captchaManager = CaptchaUtils.CaptchaManager;

//Merge two different policies into one.
captchaManager.IntelligencePolicy =
    new MultiIntelligencePolicy(
        new ResponseTimeIntelligencePolicy(TimeSpan.FromSeconds(5)),
        new FakeInputIntelligencePolicy(),
        new JavaScriptIntelligencePolicy());

Added the StorageType enum

This enum allows you to select the type of storage when using the IIntelligencePolicy. Supports two values​​ TempData and Session.

Added the AddAreaRouteValue property to the DefaultCaptchaManager class

The captcha is now supported the Areas View using the AddAreaRouteValue property. By default this is set to true, and you do not need to do anything to add support for areas.
Here's sample code:
protected void Application_Start()
{
    ...
    var captchaManager = CaptchaMvc.Infrastructure.CaptchaUtils.CaptchaManager;
    //By default true, and you do not need to do anything to add support for areas, 
    //Below is an example of what should have been done 
    //in the previous version to add support for the areas
    captchaManager.AddAreaRouteValue = false;

    var defaultCaptchaManager = (DefaultCaptchaManager)captchaManager;
    defaultCaptchaManager.ImageUrlFactory = (helper, pair) => ImageUrlFactory(defaultCaptchaManager, helper, pair);
    defaultCaptchaManager.RefreshUrlFactory = RefreshUrlFactory;
}

private string RefreshUrlFactory(UrlHelper urlHelper, KeyValuePair<string, ICaptchaValue> keyValuePair)
{
    return urlHelper.Action("Refresh", "DefaultCaptcha", new { area = "" });
}

private string ImageUrlFactory(DefaultCaptchaManager captchaManager, 
UrlHelper urlHelper, KeyValuePair<string, ICaptchaValue> keyValuePair)
{
    return urlHelper.Action("Generate", "DefaultCaptcha",
                            new RouteValueDictionary
                                {
                                    {"area", ""},
                                    {captchaManager.TokenParameterName, keyValuePair.Key}
                                });
}
If you have ideas or something does not work, send the information to me and I'll try to implement a new feature or fix the issue.
Thank you for your feedback.

Comments

hi,i had done every step by your demo above,but the image does not show in the view,why?

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]