我有一个相当标准的MVC网站联系人页面,其中包括recaptcha淘汰机器人。一切都很好。我在网站上添加了移动页面,并且由于目前希望仅在移动页面中省略控制器中的recaptcha验证的问题。
当地,当我检查无效的验证码时,本地可以很好地工作,如果不是移动设备,我会在模型中添加错误并在捕获块中捕获。如果移动不必担心验证码,只需验证模型即可。
[HttpPost, RecaptchaControlMvc.CaptchaValidator]
[ValidateAntiForgeryToken]
public ActionResult Submit(ContactFormViewModel viewModel, bool captchaValid, string captchaErrorMessage)
{
try
{
if (!captchaValid)
if (!HttpContext.Request.Browser.IsMobileDevice)
ModelState.AddModelError("captcha", captchaErrorMessage);
if (ModelState.IsValid)
{
if (viewModel.Save(viewModel))
return RedirectToAction("Thanks");
}
return View("Create");
}
catch (Exception ex)
{ //. . . error processing
}
如上所述,与Safari Useragent设置为iPhone,iPad等,以及用于各种手机的Opera Mimulator。但是,当我部署到生产时,我在提交时会出现错误,并显示了验证码错误。对我来说,这意味着.ismobiledevice测试失败。
好的 - 这就是我"解决"我的问题的方式。我认为这可能与已传递给标准Web表单使用的"提交"对象的recaptcha参数有关,因此,对于移动版本,我修改了表单以调用" MobileSubmit"方法,该方法不期望任何东西recaptcha相关。
它简单地显示为:
//
// POST: /Contact/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult MobileSubmit(ContactFormViewModel viewModel)
{
try
{
if (ModelState.IsValid)
{
if (viewModel.Save(viewModel))
return RedirectToAction("Thanks");
}
return View("Create");
}
catch (Exception ex)
{ . . .
}
,然后来自移动视图的呼叫简直就是:
@using (Html.BeginForm("MobileSubmit", "Contact", null, FormMethod.Post, new { data_ajax = "false" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Contact</legend>
@Html.Partial("_ContactFormPartial")
<br />
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</fieldset>
}
因此,仍然不确定为什么Ismobile呼叫最初不起作用,但猜测添加了recaptcha参数会导致副作用。
欢呼,Dan