[转载]Using ReCaptcha with Asp.Net MVC

[转载]Using ReCaptcha with Asp.Net MVC – Derik Whittaker – Devlicio.us – Just the Tasty Bits.

For all the debate regarding if Captcha’s are a good thing or a bad thing one thing is certain (in my book).  If you do not have some way to stop spam-bots your site will become overridden with junk in a hurry.

In an effort to reduce the amount of spam comments I am getting over at Dimecasts.net I finally got off my lazy ass and worked on implementing a Captcha.  The solution I ended up going with was reCaptcha.  I found that their setup was very easy to use and worked right out of the box.  However, there was not a lot of information on the net on how to use reCaptcha within an MVC site, only ASP.NET Webforms.  So I thought I would share my experiences and explain how I implemented reCaptcha on Dimecasts.

Step 1 – Signup for and download the reCaptcha dll from their site

Step 2 – Add reference to the Recaptcha.dll to your project

Step 3 – Create an Action Filter to handle the Captcha validation

  1. public class CaptchaValidatorAttribute : ActionFilterAttribute
  2. {
  3. private const string CHALLENGE_FIELD_KEY = “recaptcha_challenge_field”;
  4. private const string RESPONSE_FIELD_KEY = “recaptcha_response_field”;
  5. public override void OnActionExecuting(ActionExecutingContext filterContext)
  6. {
  7. var captchaChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY];
  8. var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY];
  9. var captchaValidtor = new Recaptcha.RecaptchaValidator
  10. {
  11. PrivateKey = — PUT PRIVATE KEY HERE –,
  12. RemoteIP = filterContext.HttpContext.Request.UserHostAddress,
  13. Challenge = captchaChallengeValue,
  14. Response = captchaResponseValue
  15. };
  16. var recaptchaResponse = captchaValidtor.Validate();
  17. // this will push the result value into a parameter in our Action
  18. filterContext.ActionParameters[“captchaValid”] = recaptchaResponse.IsValid;
  19. base.OnActionExecuting(filterContext);
  20. }
  21. }

Step 4 – Implement the Controller Action that will handle the form submission and Captcha validation

  1. [CaptchaValidator]
  2. [AcceptVerbs( HttpVerbs.Post )]
  3. public ActionResult CreateComment( Int32 id, bool captchaValid )
  4. {
  5. .. Do something here
  6. }

Step 5 – Create a Html Helper to build and render the Captcha control

  1. public static string GenerateCaptcha( this HtmlHelper helper )
  2. {
  3. var captchaControl = new Recaptcha.RecaptchaControl
  4. {
  5. ID = “recaptcha”,
  6. Theme = “blackglass”,
  7. PublicKey = — Put Public Key Here –,
  8. PrivateKey = — Put Private Key Here —
  9. };
  10. var htmlWriter = new HtmlTextWriter( new StringWriter() );
  11. captchaControl.RenderControl(htmlWriter);
  12. return htmlWriter.InnerWriter.ToString();
  13. }

Step 6 – Implement the logic in your view to actually render the Captcha control

  1. <:%= Html.GenerateCaptcha() %>:

Step 7 – Oh wait, there is no step 7.  You are done.

There you go, that is all that is needed to setup reCaptcha for use in a MVC application

Till next time,

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏