MVC3 ReCaptcha具有流畅的验证

时间:2012-04-13 19:04:15

标签: asp.net-mvc-3 recaptcha fluentvalidation mvcrecaptcha

我正在尝试使用FluentValidation验证ReCaptcha,但我遇到了一些问题。即使ReCaptcha.Validate返回true,ModelState也无效。为了验证ReCaptcha,我在视图模型中添加了一个字段。在检查ModelState是否有效之前,我将ReCaptcha字段设置为从ReCaptcha.Validate返回的任何内容。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Index(Over18Model model)
    {
        model.ReCaptcha = Microsoft.Web.Helpers.ReCaptcha.Validate("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

        if (ModelState.IsValid)
        {
            var table = new Prospect();
            dynamic o = new ExpandoObject();
            {
                o.FirstName = model.FirstName;
                o.LastName = model.LastName;
                o.Email = model.Email;
            }
            table.Save(o);
            return RedirectToAction("ThankYou", "Public");
        }
        ModelState.AddModelError(string.Empty, "Errors:  " + string.Join(" ; ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)));
        return View(model); 
    }

    [Validator(typeof(Over18ModelValidator))]
    public class Over18Model
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public bool ReCaptcha { get; set; }
    }

    public class Over18ModelValidator : AbstractValidator<Over18Model>
    {
        public Over18ModelValidator()
        {
            RuleFor(x => x.FirstName).NotEmpty().WithMessage("First Name is Required");
            RuleFor(x => x.LastName).NotEmpty().WithMessage("Last Name is Required");
            RuleFor(x => x.Email).NotEmpty().WithMessage("Email is Required");
            RuleFor(x => x.Email).EmailAddress().WithMessage("Invalid Email");
            RuleFor(x => x.ReCaptcha).Equal(true).WithMessage("ReCaptcha error");
        }
    }

Razor代码看起来像这样......

<div class="signUpOver18">
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()         
        @Form.TextBox(name: "FirstName", value: @Model.FirstName, labelText: "First Name:", containerClass: "fname", txtfldClass: "standard names", maxLength: 50) 
        @Form.TextBox(name: "LastName", value: @Model.LastName, labelText: "Last Name:", containerClass: "lname", txtfldClass: "standard names", maxLength: 100) 
        </div>
        @Form.Submit(value: "Enter The Sweepstakes", myClass: "btnSubmit")

        <div class="recaptchaHolder">
            @ReCaptcha.GetHtml(theme: "red", publicKey: "XXX-XXXX_XXXX-XX_") 
        </div>

        @Html.ValidationSummary(true)  
    }
</div>

1 个答案:

答案 0 :(得分:0)

如果不是必须使用流畅验证来验证验证码...尝试使用微软网络帮助程序..它可以正常运行..