在Asp.net Mvc中使用ReCaptcha
时间:2010-08-26 来源:香炮
Derik Whittaker » Using ReCaptcha with Asp.Net MVC
Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at[email protected]
Using ReCaptcha with Asp.Net MVC
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
view plaincopy to clipboardprint?
- public class CaptchaValidatorAttribute : ActionFilterAttribute
- {
- private const string CHALLENGE_FIELD_KEY = "recaptcha_challenge_field";
- private const string RESPONSE_FIELD_KEY = "recaptcha_response_field";
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- var captchaChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY];
- var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY];
- var captchaValidtor = new Recaptcha.RecaptchaValidator
- {
- PrivateKey = -- PUT PRIVATE KEY HERE --,
- RemoteIP = filterContext.HttpContext.Request.UserHostAddress,
- Challenge = captchaChallengeValue,
- Response = captchaResponseValue
- };
- var recaptchaResponse = captchaValidtor.Validate();
- // this will push the result value into a parameter in our Action
- filterContext.ActionParameters["captchaValid"] = recaptchaResponse.IsValid;
- base.OnActionExecuting(filterContext);
- }
- }
Step 4 - Implement the Controller Action that will handle the form submission and Captcha validation
view plaincopy to clipboardprint?
- [CaptchaValidator]
- [AcceptVerbs( HttpVerbs.Post )]
- public ActionResult CreateComment( Int32 id, bool captchaValid )
- {
- .. Do something here
- }
Step 5 - Create a Html Helper to build and render the Captcha control
view plaincopy to clipboardprint?
- public static string GenerateCaptcha( this HtmlHelper helper )
- {
- var captchaControl = new Recaptcha.RecaptchaControl
- {
- ID = "recaptcha",
- Theme = "blackglass",
- PublicKey = -- Put Public Key Here --,
- PrivateKey = -- Put Private Key Here --
- };
- var htmlWriter = new HtmlTextWriter( new StringWriter() );
- captchaControl.RenderControl(htmlWriter);
- return htmlWriter.InnerWriter.ToString();
- }
Step 6 - Implement the logic in your view to actually render the Captcha control
view plaincopy to clipboardprint?
- <:%= 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,
[ --- Remember to check out Dimecasts.net --- ]