Download the reCaptcha library from this link and add the reference to your web project. Sometimes its not getting copied to your bin folder, if so just to make sure do it manually.

http://code.google.com/p/recaptcha/downloads/list?q=label:aspnetlib-Latest

Signup in the reCaptcha official web site and register the web address you will be running your site. Then it will give you 2 keys as public and private.
Store them in your main web.config file appsetting section.

Changes in View

Now the background is setup. Let’s open the view file. Place the following code where you want to appear the captcha.

@Html.Raw(@Html.GenerateCaptcha())

Note: if you just use @Html.GenerateCaptcha() it will only render the script as it is. I had a nasty time struggling to tackle. 🙂 But it motivated me to write this down.

Changes in Controller

Now go to your controller add the following attributes to your action method,
[RecaptchaControlMvc.CaptchaValidatorAttribute]
Also add an additional Boolean parameter called captchaValid, which will be populated from the injected recaptcha module.

Eg:


[HttpPost]
[RecaptchaControlMvc.CaptchaValidatorAttribute]
public ActionResult Register(RegisterModel model, bool captchaValid)
{
   if (captchaValid)
   {
      if (ModelState.IsValid) {
         //Submit Data
         return RedirectToAction("Index", "Home");
      }
   }
   else{
      //Error
      ModelState.AddModelError("", "You did not type the verification word correctly please try again.");
   }
}

That is all. Now you are secured against automatic login request attempts.

Advertisements

One thought on “How to setup reCaptcha in ASP.NET MVC project

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s