Add reCAPTCHA To ASP.NET MVC Web Application

Captcha is a type of challenge-response test used in computing as an attempt to ensure that the response is generated by a person.
When building public web sites captcha is very important to avoid script and bots running on your web site.
First go to reCAPTCHA site and register for unique Key, then Download reCAPTCHA .NET Library.
Save you Public and Private keys safely.

Now, Let’s start new ASP.NET MVC 3 Web Application Project in Visual Studio 2010
Download Demo Project

Choose to create from “Internet Application” template

Add the Recaptcha dll as reference to your project.

Modify site Web.config with new keys under the appSettings section and add the keys we received before.
- ReCaptchaPrivateKey
- ReCaptchaPublicKey

Open the Controllers folder, open the AccountController file and locate the Register method, add new attribute:
[RecaptchaControlMvc.CaptchaValidatorAttribute]

And add the reCAPTCHA logic to your Registration Code
[HttpPost]
[RecaptchaControlMvc.CaptchaValidatorAttribute]
public ActionResult Register(RegisterModel model, bool captchaValid)
{
if (!captchaValid)
{
ModelState.AddModelError("", "You did not type the verification
word correctly. Please try again.");
}
else
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password,
model.Email, null, null, true, null,
out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
FormsAuthentication.SetAuthCookie(model.UserName, false);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("",
ErrorCodeToString(createStatus));
}
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
Now placed the following command in the Register.cshtml file:
@Html.Raw(Html.GenerateCaptcha())

Run your web site and enter the registration page, now you have to enter captcha before compiling the registration.

Download Demo Project