谷歌的新回收未经验证

时间:2014-12-10 22:11:15

标签: asp.net-mvc validation forms-authentication form-submit recaptcha

我错过了什么吗?我将此代码段复制到:

Automatically render the reCAPTCHA widget

我输入了我的注册数据-sitekey。当我选中复选框时,reCAPTCHA会显示并运行,但如果我没有选中复选框并提交表单,则reCAPTCHA不会停止用户提交,我的控制器会处理请求。

@model Medicaid.WebUI.ViewModels.RequestModel

<script src='https://www.google.com/recaptcha/api.js' async defer></script>

<table class="table">
<tr>
    <td>
        @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "request-form", @class = "form-horizontal" }))
        { 
        @Html.AntiForgeryToken()
         <fieldset>
          <legend>Request</legend>
           <div class="form-group">
             <label for="inputFirstName" class="col-lg-2 control-label">First Name</label>
             <div class="col-lg-10">
               <input type="text" value="@Model.FirstName" class="form-control" maxlength="50" name="FirstName" id="FirstName" placeholder="First Name" required>
             </div>
             </div>
             <div class="form-group">
             <label for="inputLastName" class="col-lg-2 control-label">Last Name</label>
              <div class="col-lg-10">
               <input type="text" value="@Model.LastName" class="form-control" maxlength="50" name="LastName" id="LastName" placeholder="Last Name" required>
               </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail" class="col-lg-2 control-label">Email</label>
                    <div class="col-lg-10">
                        <input type="email" value="@Model.Email" class="form-control" maxlength="100" name="Email" id="Email" placeholder="Email" required>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-lg-10 col-lg-offset-2"><br /><br />
                        <div class="g-recaptcha" data-sitekey="IenterMykeyHere"></div><br /><br />
                        <button type="submit" class="btn btn-primary">Submit Request</button>
                    </div>
                </div>
            </fieldset>
        }
    </td>
</tr>
</table>
<br /><br />
@Html.ValidationSummary()

1 个答案:

答案 0 :(得分:0)

我遇到了这个问题并重新阅读了文档,并按照以下方式进行了解决:

function onBegin() {
    $("input[type='hidden']").val(grecaptcha.getResponse());
}

您必须从MVC html帮助程序向AjaxOptions对象的“onBegin”参数添加一个javascript函数 - 它会将窗口小部件响应的值复制到您需要在表单中声明的​​隐藏变量,因此提交按钮将其发送到控制器。不要忘记在“RequestModel”视图模型中添加一个具有相同名称的变量,以便mvc模型绑定传递它,然后您就可以在服务器中知道用户是否单击了按钮

@using (Ajax.BeginForm("NewRecaptchaVerify", "ReCaptcha", new AjaxOptions
{
    HttpMethod = "Post",
    OnSuccess = "onSuccess",
    OnFailure = "onFailure",
    OnBegin = "onBegin",
    OnComplete = "onComplete"
}))
{
    @Html.HiddenFor(m => m.Response)
    <div class="g-recaptcha" data-sitekey="your-site-key"></div>
    <input type="submit" id="btnSubmit" value="Submit" />
}

(对于记录,我现在面临的问题是,在服务器中,在小部件提示用户输入文本输入的情况下,在单击按钮后 - 无论他/她是否输入文本。在我看来,除了在服务器中对小部件的api执行GET请求之外,我将无法解决问题,因为他们将其放入文档中:https://www.google.com/recaptcha/api/siteverify?secret=your_secret&response=response_string&remoteip=user_ip_address