I am trying to get the new Google reCaptcha working in my ASP.NET project and I am having problems with its new "I'm not a robot."
I had an old one there, and after a lot of research on developers.google.com everything looks the same (they even point me to downloading the same library - 1.0.5). So, I got new keys and entered them, and it works, but it looks just like the old reCaptcha.
Has anyone got a new one to work with their ASP.Net? What am I missing?
EDIT:
So, playing in a test application and browsing some other websites, I found that if I create such a page:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>reCAPTCHA demo: Simple page</title> <script src="https://www.google.com/recaptcha/api.js" async defer></script> </head> <body> <form id="form1" runat="server" action="?" method="POST"> <div> <div class="g-recaptcha" data-sitekey="My Public Key"></div> <br/> <asp:Button ID="Button1" runat="server" Text="Submit" /> </div> </form> </body> </html>
And then in my code-behind (Button1_Click), I do this:
Dim Success As Boolean Dim recaptchaResponse As String = request.Form("g-recaptcha-response") If Not String.IsNullOrEmpty(recaptchaResponse) Then Success = True Else Success = False End If
recaptchaResponse will be either empty or populated depending on whether they are a bot or not. The problem is that now I need to accept this answer and send it to Google using my private key so that I can make sure that the answer was not provided by the bot in my code, but I cannot figure out how to do it. I tried this (instead of Success = True ):
Dim client As New System.Net.Http.HttpClient() client.BaseAddress = New Uri("https://www.google.com/recaptcha/") client.DefaultRequestHeaders.Accept.Clear() client.DefaultRequestHeaders.Accept.Add(New Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")) Dim response As Net.Http.HttpResponseMessage = Await client.GetAsync("api/siteverify?secret=My Private key&response=" + recaptchaResponse) If (response.IsSuccessStatusCode) Then Dim CaptchResponse As ReCaptchaModel = Await response.Content.ReadAsAsync(Of ReCaptchaModel)() Success = CaptchResponse.success Else Success = False End If
But I couldnβt figure out how to work with the asynchronous file, and I canβt find anything in that of ReCaptchaModel , so I found another way to call the web service and get the json response and tried this instead:
Dim request As Net.WebRequest = Net.WebRequest.Create("https://www.google.com/recaptcha/") Dim Data As String = "api/siteverify?secret=My Private Key&response=" + recaptchaResponse request.Method = "POST" request.ContentType = "application/json; charset=utf-8" Dim postData As String = "{""data"":""" + Data + """}" 'get a reference to the request-stream, and write the postData to it Using s As IO.Stream = request.GetRequestStream() Using sw As New IO.StreamWriter(s) sw.Write(postData) End Using End Using 'get response-stream, and use a streamReader to read the content Using s As IO.Stream = request.GetResponse().GetResponseStream() Using sr As New IO.StreamReader(s) 'decode jsonData with javascript serializer Dim jsonData = sr.ReadToEnd() Stop End Using End Using
But that just gives me the content of the webpage at https://www.google.com/recaptcha . Not what I want. The Google page is not very useful, and I am fixated on where to go. I need help calling a Google validation service, or if someone found another way to do this from ASP.NET.