Advertisement
Guest User

recaptcha

a guest
Dec 22nd, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. [HttpPost]
  2. public void Method(MyData data, string challenge, string response)
  3. {
  4.     string recaptchaValidationUrl = "http://www.google.com/recaptcha/api/verify";
  5.     string privateKey = "MY_RECAPTCHA_PRIVATE_KEY";
  6.  
  7.     if (!CheckRecaptcha(recaptchaValidationUrl, request.RcChallenge, request.RcResponse, privateKey,
  8.     HttpContext.Current.Request.UserHostAddress))
  9.     {
  10.         return new FindTechResResponse { ErrorMessage = "Invalid captcha code" };
  11.     }
  12. }
  13.  
  14. public static bool CheckRecaptcha(string recaptchaValidationUrl, string challenge, string response, string privateKey, string clientIp)
  15. {
  16.     using (WebClient webClient = new WebClient())
  17.     {
  18.         NameValueCollection data = new NameValueCollection();
  19.         data["privatekey"] = privateKey;
  20.         data["remoteip"] = clientIp;
  21.         data["challenge"] = challenge;
  22.         data["response"] = response;
  23.  
  24.         byte[] responseBytes = webClient.UploadValues(recaptchaValidationUrl, "POST", data);
  25.         string responseString = Encoding.UTF8.GetString(responseBytes);
  26.         string isSolvedString = responseString.Split('\n')[0];
  27.         bool isSolved = bool.Parse(isSolvedString);
  28.         return isSolved;
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement