从2

时间:2015-10-25 10:20:30

标签: captcha recaptcha

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

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

更新..

要在表单中实现Captcha,我使用以下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>

因为我需要让上面的表单中的Captcha验证并在按钮点击时获得响应,但是现在我正在使用<script src="http://www.google.com/recaptcha/api/challenge?k=6Lduiw8TAAAAAOZRYAWFUHgFw9_ny5K4-Ti94cY9"></script>,所以不要在表单中获取Captcha ..请帮助我获取那..这里是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");
                    }
                });
            }

            });
    });

请帮帮我..谢谢..

1 个答案:

答案 0 :(得分:3)

您必须在服务器端的“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]

因此,您必须在服务器端方法上发布它们,如下所示:

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);
    }
    if (result.StartsWith("true"))
    {
        return true;
    }
    else
    {
        return false;
    }
}