Click here to Skip to main content
12,448,720 members (52,563 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

20.2K views
5 bookmarked
Posted

Adding reCAPTCHA to asp.net MVC

, 15 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
These are just a few steps, as the latest reCAPTCHA library has all the pieces you need for asp.net MVC: Go to Get reCAPTCHA, which gives you the public and private keys you need to use reCAPTCHA with your site Download the latest .net library at: http://code.google.com/p/recaptcha/downloads/list.

These are just a few steps, as the latest reCAPTCHA library has all the pieces you need for asp.net MVC:

  • Go to Get reCAPTCHA, which gives you the public and private keys you need to use reCAPTCHA with your site
  • Download the latest .net library at: http://code.google.com/p/recaptcha/downloads/list. Currently: dotnet-1.0.4.0
  • Add <%= Html.GenerateCaptcha()%> to the form you want to protect with the reCAPTCHA. Naturally you have to add the namespace for the extension to be recognized, some of the options:
    • Add the namespace to the web.config:
      <pages>
            <namespaces>
      ...
              <add namespace="Recaptcha" />
    • Add the namespace at the view:
      <%@ Import Namespace="Recaptcha" %>
    • Add your own extension method that wraps it. I changed the signature to return MvcHtmlString to prevent double encoding when using it with “<%:” instead of “<%=”
      public static MvcHtmlString GenerateCaptcha(this HtmlHelper htmlHelper)
      {
          var html = Recaptcha.RecaptchaControlMvc.GenerateCaptcha(htmlHelper);
          return MvcHtmlString.Create(html);
      }
  • Add the RecaptchaControlMvc.CaptchaValidator attribute to your controller. Also add parameters named captchaIsValid and captchaErrorMessage. Just like:
    [RecaptchaControlMvc.CaptchaValidator]
    public ActionResult MyMethod(Something else, bool captchaValid, 
       string captchaErrorMessage)
    {
    // do something if (!captchaValid)
    }
  • Configure your keys. Some options:
    • Add to appsettings in the web.config, with entries named: RecaptchaPublicKey and RecaptchaPrivateKey
    • Set at Application Start:
      RecaptchaControlMvc.PrivateKey = privKey;
      RecaptchaControlMvc.PublicKey = pubKey;

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

eglasius
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
AnswerOr create an extension method Pin
Damian_GCoders1-Mar-11 4:08
memberDamian_GCoders1-Mar-11 4:08 
General[My vote of 1] not an article.. Pin
Seishin#15-Nov-10 21:21
memberSeishin#15-Nov-10 21:21 
GeneralRe: [My vote of 1] not an article.. [modified] Pin
eglasius16-Nov-10 7:17
membereglasius16-Nov-10 7:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160811.3 | Last Updated 15 Oct 2010
Article Copyright 2010 by eglasius
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid