我正在使用MvcRecaptcha来阻止ASP.NET MVC2.0站点上复杂的未经身份验证的客户端表单的机器人发布。
即使表单的某些输入是不正确的,我也只希望从未经身份验证的客户端获得一个正确的CAPTCHA条目。
在成功输入后,我曾尝试在视图中使用Session["CaptchaSuccess"] = true;变量来抑制Html.GenerateCaptcha(),但是[HttpPost]视图上的[CaptchaValidator]属性会导致错误,因为它自然需要一些ReCaptcha表单输入。
要实现这一点,包括在移动浏览器上,最简单的方法是什么?
发布于 2010-08-24 06:45:38
通过修改[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));
}https://stackoverflow.com/questions/3551504
复制相似问题