經過一些測試,看起來你可以做前端部分。數據回調函數不會被調用,直到谷歌確信你是一個人,如果谷歌不確定,然後它加載「選擇哪些瓷磚有一件事情」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;
}
}
你見過https://developers.google.com/recaptcha/docs/verify的ASHX和Ajax? – kichik
是的 - 但是不可見實現頁面上的文檔並不清楚是否需要驗證,以及一些人們沒有任何代碼驗證的示例(https://jsfiddle.net/jayh99/dp1cLh28/),而其他做(http://www.pinnacleinternet.com/installing-invisible-recaptcha/)所以 - 是否有必要? – user6383418
當然是。什麼能阻止某人在沒有它的情況下腳本化你的表單? – kichik