2010-08-23 3 views
0

ASP.NET MVC 2.0 사이트에서 복잡한 인증되지 않은 클라이언트 양식의 봇 포스트를 막으려면 MvcRecaptcha을 사용하고 있습니다.MvcReCaptcha로 CAPTCHA를 한 번만 요청하십시오.

양식의 입력 중 일부가 올바르지 않더라도 인증되지 않은 클라이언트에서 올바른 CAPTCHA 항목을 하나만 필요로합니다.

나는 성공 항목 다음 내보기에 Html.GenerateCaptcha()을 억제하기 위해 Session["CaptchaSuccess"] = true; 변수를 사용하여 시도,하지만 자연스럽게 일부 reCAPTCHA를 양식 입력을 필요로하기 때문에 내 [HttpPost]보기에 [CaptchaValidator] 속성의 존재는 오류가 발생합니다.

모바일 브라우저를 비롯하여이를 안정적으로 달성하는 가장 간단한 방법은 무엇입니까?

답변

0

CaptchaSuccessFieldKey는 상수 문자열 값 "CaptchaSuccess"로 지칭 [CaptchaValidatorAttribute] OnActionExecuting 방법, 수정함으로써 해결했다

public override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 
      bool? bCaptchaSuccess = filterContext.HttpContext.Session[CaptchaSuccessFieldKey] as bool?; 
      if (bCaptchaSuccess.HasValue && bCaptchaSuccess.Value) 
      { 
       filterContext.ActionParameters["captchaValid"] = true; 
      } 
      else 
      { 

       var captchaChallengeValue = filterContext.HttpContext.Request.Form[ChallengeFieldKey]; 
       var captchaResponseValue = filterContext.HttpContext.Request.Form[ResponseFieldKey]; 
       var captchaValidtor = new Recaptcha.RecaptchaValidator 
              { 
               PrivateKey = ConfigurationManager.AppSettings["ReCaptchaPrivateKey"], 
               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); 

      // Add string to Trace for testing 
      //filterContext.HttpContext.Trace.Write("Log: OnActionExecuting", String.Format("Calling {0}", filterContext.ActionDescriptor.ActionName)); 
     }