Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. <%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>
  2.  
  3. <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  4.  
  5. <div class="g-recaptcha" data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"></div>
  6.  
  7. <recaptcha:RecaptchaControl
  8. ID="recaptcha"
  9. runat="server"
  10. PublicKey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"
  11. PrivateKey="_My private key taken from the reCaptcha API_"
  12. />
  13.  
  14. <html>
  15. <head>
  16. <script src='https://www.google.com/recaptcha/api.js'></script>
  17. </head>
  18. <body>
  19. <form>
  20.  
  21. public partial class page : System.Web.UI.Page
  22. {
  23. private string CAPTCHA_SECRET_KEY = @"6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe"; // WARNING: FAKE SECRET KEY
  24. protected void Page_Load(object sender, EventArgs e)
  25. {
  26. if(IsPostBack)
  27. {
  28. String username = unameInput.Text;
  29. String password = pwordInput.Text;
  30.  
  31. if (validate())
  32. {
  33. // ...
  34. }
  35. }
  36. }
  37.  
  38. // Thanks to http://www.thatsoftwaredude.com/content/6235/implementing-googles-no-captcha-recaptcha-in-aspnet
  39. private bool validate()
  40. {
  41. string url = @"https://www.google.com/recaptcha/api/siteverify";
  42. WebRequest request = WebRequest.Create(url);
  43. string postData = string.Format("secret={0}&response={1}&remoteip={2}", CAPTCHA_SECRET_KEY, Request["g-recaptcha-response"], Request.UserHostAddress);
  44.  
  45. request.Method = "POST";
  46. request.ContentType = "application/x-www-form-urlencoded";
  47. request.ContentLength = postData.Length;
  48.  
  49. StreamWriter writer = new StreamWriter(request.GetRequestStream());
  50. writer.Write(postData);
  51. writer.Close();
  52.  
  53. StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream());
  54. string responseData = reader.ReadToEnd();
  55.  
  56. JavaScriptSerializer jss = new JavaScriptSerializer();
  57. CaptchaResponse cResponse = jss.Deserialize<CaptchaResponse>(responseData);
  58. return cResponse.success;
  59. }
  60. catch(WebException)
  61. {
  62. return true; // TODO: Change to false when releasing
  63. }
  64. }
  65. class CaptchaResponse
  66. {
  67. public bool success { get; set; }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement