2017-09-22 123 views
0

我遵循this page 的說明執行隱形recaptcha。一切都很好,但我怎麼知道它在工作?有沒有辦法迫使一個錯誤來測試它?我是否需要驗證Google Invisible reCaptcha的結果

此外,文檔不清楚上面的頁面,但有些地方有額外的代碼來驗證用戶的「回覆」(它是不可見的,所以我不知道這是什麼答案) - 所以我需要添加額外的後端邏輯,以this端點與不可見的reCaptcha生成的令牌和我的密鑰?

當用戶點擊提交隱形的驗證碼時會發生什麼?在API中做什麼來返回令牌?什麼是令牌?那麼網站驗證api然後做什麼來確定它的一個人?爲什麼在使用reCAPTCHA V2(可見點擊一次)時不需要額外驗證?

+0

你見過https://developers.google.com/recaptcha/docs/verify的ASHX和Ajax? – kichik

+0

是的 - 但是不可見實現頁面上的文檔並不清楚是否需要驗證,以及一些人們沒有任何代碼驗證的示例(https://jsfiddle.net/jayh99/dp1cLh28/),而其他做(http://www.pinnacleinternet.com/installing-invisible-recaptcha/)所以 - 是否有必要? – user6383418

+0

當然是。什麼能阻止某人在沒有它的情況下腳本化你的表單? – kichik

回答

1

經過一些測試,看起來你可以做前端部分。數據回調函數不會被調用,直到谷歌確信你是一個人,如果谷歌不確定,然後它加載「選擇哪些瓷磚有一件事情」reCaptcha肯定。一旦reCaptcha api確定它是一個人,數據回調函數被觸發 - 那時你可以做進一步驗證,以確保你在回調期間收到的令牌是谷歌實際發送的令牌,而不是試圖通過點擊回調函數來欺騙你 - 所以你從服務器端進行處理以進一步驗證。下面是一個C#ashx的處理程序示例 - 用於驗證

function onTestSubmit(token) { 
     $.ajax({ 
      type: "POST", 
      url: "testHandler.ashx", 
      data: { token: token }, 
      success: function (response) { 
       if (response == "True") { 
        //do stuff to submit form 
       } 
      } 
     });    
    } 

而且

public class testHandler : IHttpHandler { 

    public void ProcessRequest (HttpContext context) { 
     context.Response.ContentType = "text/plain"; 
     string token = context.Request.Form["token"]; 
     bool isCaptchaValid = ReCaptcha.Validate(token); 

     context.Response.Write(isCaptchaValid.ToString()); 
    } 


    public bool IsReusable { 
     get { 
      return false; 
     } 
    } 

    } 

    public class ReCaptcha 
    { 
     private static string URL = 
    "https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}"; 
     private static string SECRET = "shhhhhhhhhhhhhhSecretTOken"; 

     public bool Success { get; set; } 
     public List<string> ErrorCodes { get; set; } 

     public static bool Validate(string encodedResponse) 
     { 
     if (string.IsNullOrEmpty(encodedResponse)) return false; 
     var client = new System.Net.WebClient(); 
     var googleReply = client.DownloadString(string.Format(URL, SECRET, encodedResponse)); 
     var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
     var reCaptcha = serializer.Deserialize<ReCaptcha>(googleReply); 
     return reCaptcha.Success; 
    } 
}