0

MvcRecaptchaを使用して、 ASP.NETMVC2.0サイト上の複雑な認証されていないクライアントフォームへのボット投稿を防止しています。

フォームの入力の一部が正しくない場合でも、認証されていないクライアントからの正しいCAPTCHAエントリを1つだけ要求したいと思います。

エントリが成功した後、Session["CaptchaSuccess"] = true;変数を使用してビューで抑制しようとしましたが、ビューに属性が存在すると、当然、ReCaptchaフォームの入力が必要になるため、エラーが発生します。Html.GenerateCaptcha()[CaptchaValidator][HttpPost]

モバイルブラウザを含め、これを確実に達成するための最も簡単な方法は何ですか?

4

1 に答える 1

0

定数文字列値「CaptchaSuccess」を参照する[CaptchaValidatorAttribute]OnActionExecuting メソッドを変更することで解決されました。CaptchaSuccessFieldKey

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));
        }
于 2010-08-23T22:45:37.777 に答える