从2切换Google reCaptcha版本1

问题描述 投票:2回答:1

我已经成功设计并实现了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");
                    }
                });
            }

            });
    });

请帮助我..谢谢..

captcha recaptcha
1个回答
3
投票

您必须在服务器端的“ http://www.google.com/recaptcha/api/verify”处验证验证码。此参数为:

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]

因此,您必须像这样将它们发布到服务器端方法上:

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");
}
© www.soinside.com 2019 - 2024. All rights reserved.