从 2 切换 Google reCaptcha 版本 1

Switching Google reCaptcha Version 1 from 2

我已经成功设计并实施了 Google reCaptcha 版本 2,但现在我的经理希望它是版本 1,并且需要输入和验证数字。有没有办法从后来切换到以前,即 - 从 2 到 1。我正在使用以下库进行 reCaptcha:

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

更新..

为了在表单内实现验证码,我使用以下 HTML..

 <form class="contact_form" action="#" method="post" name="contact_form">
                <div class="frm_row">
                    <label id="lblmsg" />
                    <div class="clear">
                    </div>
                </div>
                <div class="g-recaptcha" data-sitekey="6Lduiw8TAAAAAOZRYAWFUHgFw9_ny5K4-Ti94cY9"></div>

                <div class="login-b">
                    <span class="button-l">
                      <input type="button" id="Captcha" name="Submit" value="Submit" />
                    </span>
                    <div class="clear"> </div>
                </div>
            </form>

因为我需要在上面的表单中获取验证码以验证并在单击按钮时获得响应但是因为现在我正在使用 <script src="http://www.google.com/recaptcha/api/challenge?k=6Lduiw8TAAAAAOZRYAWFUHgFw9_ny5K4-Ti94cY9"></script> ,所以没有在表单中获取验证码..请帮助我得到那个..这里还有 Jquery Ajax 代码来发送服务器端代码的请求..

  $(document).ready(function () {

        alert("hii1");

        $('#Captcha').click(function () {

            alert("Hii2");

            if ($("#g-recaptcha-response").val()) {

                alert("Hii3");

                var responseValue = $("#g-recaptcha-response").val();

                alert(responseValue);

                $.ajax({
                    type: 'POST',
                    url: 'http://localhost:64132/ValidateCaptcha',
                    data: JSON.stringify({ "CaptchaResponse": responseValue }),
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',  // Set response datatype as JSON
                    success: function (data) {
                        console.log(data);
                        if (data = true) {
                            $("#lblmsg").text("Validation Success!!");
                        } else {

                            $("#lblmsg").text("Oops!! Validation Failed!! Please Try Again");
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert("Error");
                    }
                });
            }

            });
    });

请帮助我..谢谢..

您必须在服务器端的“http://www.google.com/recaptcha/api/verify”处验证 reCaptcha。 这个的参数是:

privatekey: Your Private Key
remoteip: User's IP Address
challenge: Value of input[name=recaptcha_response_field]
response: Value of input[name=recaptcha_challenge_field]

因此,您必须像这样在您的服务器端方法中 post 它们:

cshtml 文件:

var recaptchaResponseField=$("input[name=recaptcha_response_field]").val();
var recaptchaChallengeField=$("input[name=recaptcha_challenge_field]").val();

// ajax block
$.ajax({
    url: '/api/VerifyReCaptcha/',   // your Server-side method
    type: 'POST',
    data: {
        ipAddress: '@Request.ServerVariables["REMOTE_ADDR"]',
        challengeField: recaptchaChallengeField,
        responseField: recaptchaResponseField
    },
    dataType: 'text',
    success: function (data) {
        // Do something
    },

由于您使用的是.NET,所以C#代码示例如下:

cs 文件:

using System.Net;
using System.Collections.Specialized;

[HttpPost]
public bool VerifyReCaptcha(string ipAddress, string challengeField, string responseField)
{
    string result = "";
    using (WebClient client = new WebClient())
    {
        byte[] response =
        client.UploadValues("http://www.google.com/recaptcha/api/verify", new NameValueCollection()
        {
           { "privatekey", "{Your private key}" },
           { "remoteip", ipAddress },
           { "challenge", challengeField },
           { "response", responseField },
        });

        result = System.Text.Encoding.UTF8.GetString(response);
    }

    return result.StartsWith("true");
}