使用 MvcReCaptcha 仅请求一次验证码

发布于 2024-09-15 15:50:12 字数 449 浏览 9 评论 0原文

我正在使用 MvcRecaptcha 来防止机器人在 ASP.NET MVC 2.0 网站上发布复杂的未经身份验证的客户端表单。

我只想要求未经身份验证的客户端输入一个正确的验证码,即使某些表单的输入不正确。

在我看来,在成功输入后,我尝试使用 Session["CaptchaSuccess"] = true; 变量来抑制 Html.GenerateCaptcha() ,但存在 <我的 [HttpPost] 视图上的 code>[CaptchaValidator] 属性会导致错误,因为它自然需要一些 ReCaptcha 表单输入。

可靠地实现这一目标的最简单方法是什么(包括在移动浏览器上)?

I'm using MvcRecaptcha to prevent bot posts for a complex unauthenticated client form on an ASP.NET MVC 2.0 site.

I only want to require one correct CAPTCHA entry from an unauthenticated client, even if some of the form's inputs are incorrect.

I have tried using a Session["CaptchaSuccess"] = true; variable to suppress Html.GenerateCaptcha() in my view following a successful entry, but the presence of the [CaptchaValidator] attribute on my [HttpPost] view causes an error because it naturally requires some ReCaptcha form inputs.

What is the simplest way to achieve this reliably, including on mobile browsers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中的蝴蝶 2024-09-22 15:50:12

通过修改[CaptchaValidatorAttribute] OnActionExecuting方法解决,其中CaptchaSuccessFieldKey指常量字符串值“CaptchaSuccess”:

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));
        }

Solved by modifying the [CaptchaValidatorAttribute] OnActionExecuting method, where CaptchaSuccessFieldKey refers to the constant string value "CaptchaSuccess":

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));
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文