“我不是机器人”谷歌reCAPTCHA 2验证码的使用方法

发布时间:2017-07-31

新的reCAPTCHA被Google称作没有验证码的验证码("No CAPTCHA reCAPTCHA"),他让用户只需要简单的勾选就可以确认你是真实用户而非恶意机器人,操作非常简单。

noCAPTCHA只提供了一个复选框,里面写着“我不是机器人”。当你打钩之后,谷歌就能利用“风险分析引擎”进行一系列无缝检查,以此来判断你是否是真人。

如果noCAPTCHA认为你是真人,那就不用再做什么了,这确实很容易。如果noCAPTCHA认为你不是真人,它就会要求你填一个传统的CAPTCHA字符串或更先进的字符串,比如从一组图片中挑选出正确的图片。


相关网址:(reCAPTCHA无法在大陆使用)

https://www.google.com/recaptcha/intro/invisible.html

https://developers.google.com/recaptcha/

https://www.google.com/recaptcha/admin


1.打开连接https://www.google.com/recaptcha/admin,登录Google账号。

2.申请一对Key,分别为:Site key(网站密钥,显示在网页中),Secret key(安全密钥,用于验证)

3.在你的网站HTML中添加:

方法一:

<div class="g-recaptcha" data-sitekey="网站密钥"></div>

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

方法二:

<div class="g-recaptcha" id="html_element"></div>

<script type="text/javascript">
    var  onloadCallback = function () {
        grecaptcha.render('html_element', {
            'sitekey': '网站密钥'
        });
    };
</script>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>

4.服务器端添加验证代码:

public class ReCaptcha
{
    //检查是否通过验证
    public static bool Check(string captcha)
    {
        try
        {
            string res = Post("https://www.google.com/recaptcha/api/siteverify", captcha);
            if (!string.IsNullOrEmpty(res))
            {
                JObject obj = JObject.Parse(res);
                return (bool)obj["success"];
            }
        }
        catch (Exception ex)
        {
        }
        return false;
    }

    public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    { 
        return true;
    }
    
    public static string Post(string url, string content)
    {
        System.GC.Collect();

        string result = "";

        HttpWebRequest request = null;
        HttpWebResponse response = null;
        Stream reqStream = null;

        try
        {
            ServicePointManager.DefaultConnectionLimit = 200;
            if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback =
                        new RemoteCertificateValidationCallback(CheckValidationResult);
            }

            request = (HttpWebRequest)WebRequest.Create(url);

            request.Method = "POST";

            request.ContentType = "text/xml";
            byte[] data = System.Text.Encoding.UTF8.GetBytes(content);
            request.ContentLength = data.Length;

            reqStream = request.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            reqStream.Close();

            response = (HttpWebResponse)request.GetResponse();

            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            result = sr.ReadToEnd().Trim();
            sr.Close();
        }
        catch (System.Threading.ThreadAbortException e)
        {
            System.Threading.Thread.ResetAbort();
        }
        catch (WebException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            if (response != null)
            {
                response.Close();
            }
            if (request != null)
            {
                request.Abort();
            }
        }
        return result;
    }
}