Captcha MVC code

main thread :http://captchamvc.codeplex.com/

CaptchaMVC will implement your web MVC3 applications easier and more reliable protection.

Example:
Captcha.PNG

In new version:

  • Add attribute CaptchaVerifyAttribute
  • Add more setting for captcha
  • You can override the encryption method
  • You can specify the character set for the captcha
  • Fix some bug and improved performance

How to use

Step 1: Add your “CaptchaPass” and “CaptchaSalt” to your web.config file in appsettings section:

    <!--This is important-->    <!--CaptchaPass is any string.-->    <!--CaptchaSalt is Base64String-->    <!--*****************************************************-->    <add key="CaptchaPass" value="Password"/>    <add key="CaptchaSalt" value="ZHNmZ2JzZHJlZmR3YXRnc2Q="/>     <!--*****************************************************-->

For ajax refresh the data you need add:

 <!--It is important for ajax updating captcha.-->    <!--*****************************************************-->    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>     <!--*****************************************************-->

Step 2: Implement the logic in your view to actually render the MVCCaptcha control:

//the second parameter is the text refresh-links//the third parameter is the text info //the fourth parameter is the number of characters        @CaptchaMVC.HtmlHelpers.CaptchaHelper.Captcha(Html, "Refresh", "Input symbols", 5)

Step 3: Check the entered data:

Method 1.
You can use extension methods for ControllerBase.

       [HttpPost]        public ActionResult NotAttribute(SimpleModel simpleModel)        {            if (this.IsCaptchaVerify("Captcha is not valid"))            {                TempData["Message"] = "Captcha is valid";                return View();            }

            TempData["Message"] = "Captcha is not valid";            return View(simpleModel);        }

Method 2.
You can use attribute CaptchaVerifyAttribute.

//In ModelState will be add error for captcha        [HttpPost]        [CaptchaVerify("Captcha is not valid")]        public ActionResult UseAttribute(SimpleModel simpleModel)        {            if (ModelState.IsValid)            {                TempData["Message"] = "Captcha is valid";                return View();            }            TempData["Message"] = "Captcha is not valid";            return View(simpleModel);        }

How to Customise

You can completely change the displayed image.
To do this you must implement the interface CaptchaMVC.IGenerateImage.
And to add to the web.config file in appsettings section:

<!--CaptchaIGenerate - is the full class name with the realization IGenerateImage.--><add key="CaptchaIGenerate" value="WebTestCaptcha.CaptchaCustomize.MyImage"/>

You can change encryption method.
To do this you must implement the interface CaptchaMVC.IEncryption.
And to add to the web.config file in appsettings section:

<!--CaptchaIEncryption - is the full class name with the realization IEncryption.--><add key="CaptchaIEncryption" value="WebTestCaptcha.CaptchaCustomize.MyEncryptor"/>

You can change chars for show in captcha. To do this, add to web.config:

<!--CaptchaChars is chars for show in captcha. Default is qwertyuipasdfghjklzcvbnm123456789.--><add key="CaptchaChars" value="0123456789"/>

(For details, see the examples)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s