文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>在Asp.net Mvc中使用ReCaptcha

在Asp.net Mvc中使用ReCaptcha

时间:2010-08-26  来源:香炮

Technorati 标签: asp.net mvc recaptcha

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?

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

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

view plaincopy to clipboardprint?

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

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

view plaincopy to clipboardprint?

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

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

view plaincopy to clipboardprint?

  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,

[ --- Remember to check out Dimecasts.net --- ]

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载