【问题标题】:Google's new recaptcha not validatingGoogle 的新验证码未验证
【发布时间】:2014-12-10 22:11:14
【问题描述】:

我错过了什么吗?我把这段代码 sn-p 复制过来了:

Automatically render the reCAPTCHA widget

我输入了我注册的数据站点密钥。当我选中该框时,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()

【问题讨论】:

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


    【解决方案1】:

    我遇到了这个问题并重新阅读了文档,结果如下:

    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&amp;response=response_string&amp;remoteip=user_ip_address

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-08
      • 1970-01-01
      • 2014-09-29
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 2012-05-21
      • 2012-04-18
      相关资源
      最近更新 更多