Click here to Skip to main content
15,888,816 members
Articles / Web Development / HTML

Adding reCAPTCHA to ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
3.50/5 (4 votes)
15 Oct 2010CPOL 25.8K   5   3
How to add reCAPTCHA to ASP.NET MVC

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:
      ASP.NET
      <pages>
            <namespaces>
      ...
              <add namespace="Recaptcha" />
    • Add the namespace at the view:
      ASP.NET
      <%@ 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 “<%=
      C#
      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:
    C#
    [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:
      C#
      RecaptchaControlMvc.PrivateKey = privKey;
      RecaptchaControlMvc.PublicKey = pubKey;
This article was originally posted at http://eglasius.blogspot.com/feeds/posts/default

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerOr create an extension method Pin
Damian_GCoders1-Mar-11 4:08
Damian_GCoders1-Mar-11 4:08 
General[My vote of 1] not an article.. Pin
Seishin#15-Nov-10 21:21
Seishin#15-Nov-10 21:21 
GeneralRe: [My vote of 1] not an article.. [modified] Pin
eglasius16-Nov-10 7:17
eglasius16-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.