How to add the updated (2021) version of ReCaptcha v3 in vb.net

By Mark Sarpy Jr - February 12, 2021


Updates to the change google made to reCAPTCHA v3. Those changes were reCAPTCHA tokens expire after two minutes. So now you would need to make sure to call execute when the user takes the action (button click) rather than on page load.



aspx code

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="https://www.google.com/recaptcha/api.js"></script> </head> <body> <form id="form1" runat="server"> <div> <asp:HiddenField ID="gRecaptchaResponse" runat="server" /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> <br /> <button class="g-recaptcha" data-sitekey="<%= ConfigurationManager.AppSettings("siteKey") %>" data-callback='onSubmit' data-action='submit'>Submit</button> <asp:Button ID="Button1" runat="server" style="display:none"/> </div> </form> <script> function onSubmit(token) { document.getElementById('gRecaptchaResponse').value = token; document.getElementById('Button1').click(); } </script> </body> </html>

vb code

Imports System.IO Imports System.Net Imports System.Web.Script.Serialization Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Dim url As String Dim secretKey As String = MysecretKey Dim recaptcha_response = gRecaptchaResponse.Value Label1.Text = "" ' Create a request using a URL that can receive a post. Dim Myrequest As WebRequest = WebRequest.Create("https://www.google.com/recaptcha/api/siteverify") ' Set the Method property of the request to POST. Myrequest.Method = "POST" ' Create POST data and convert it to a byte array. Dim postData As String = "secret=" & secretKey & "&response=" & recaptcha_response Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData) ' Set the ContentType property of the WebRequest. Myrequest.ContentType = "application/x-www-form-urlencoded" ' Set the ContentLength property of the WebRequest. Myrequest.ContentLength = byteArray.Length ' Get the request stream. Dim dataStream As Stream = Myrequest.GetRequestStream() ' Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length) ' Close the Stream object. dataStream.Close() ' Get the response. Dim response As WebResponse = Myrequest.GetResponse() ' Display the status. Console.WriteLine(CType(response, HttpWebResponse).StatusDescription) ' Get the stream containing content returned by the server. ' The using block ensures the stream is automatically closed. Using dataStream1 As Stream = response.GetResponseStream() ' Open the stream using a StreamReader for easy access. Dim reader As New StreamReader(dataStream1) ' Read the content. Dim responseFromServer As String = reader.ReadToEnd() ' Response to string. Dim jsonData As String = responseFromServer Dim JsonResults As Object = New JavaScriptSerializer().Deserialize(Of Object)(jsonData) If JsonResults("success") = True Then 'Submit request 'For testing only '---------------------------------------------------------------------- Label1.Text = "<br/>" Label1.Text &= "success: " & JsonResults("success") & "<br/>" Label1.Text &= "score: " & JsonResults("score") & "<br/>" Label1.Text &= "action: " & JsonResults("action") & "<br/>" Label1.Text &= "challenge_ts: " & JsonResults("challenge_ts") & "<br/>" '---------------------------------------------------------------------- Else 'Stop submit 'For testing only '---------------------------------------------------------------------- Label1.Text &= "success: " & JsonResults("success") & "<br/>" Label1.Text &= "errorcodes: " & JsonResults("error-codes")(0) & "<br/>" '---------------------------------------------------------------------- End If End Using ' Clean up the response. response.Close() End Sub


How to add ReCaptcha v3 in vb.net

By Mark Sarpy Jr - October 1, 2019


Sometime ago I tried to incorporate ReCaptcha with vb.net and I couldn't figure out exactly how to do it based on the documentation that's provided by Google. There's lots of tutorials out there that shows you how to do it for PHP but for VB I just couldn't find anything. One of the main issues I dealt with was figuring out that I had to past a post value that's created in the token that needed to be passed to the code behind that is used to validate if they are a bot or not. Once I figured that out It pretty much made everything more understandable. The way I was able to do this was looking at examples in PHP then figure out how to do the same in vb.net. Below you will find a video to show you how I did it and the example code.



aspx code

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="https://www.google.com/recaptcha/api.js?render=SITE_KEY_HERE"></script> </head> <body> <form id="form1" runat="server"> <div> <input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response"/> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> <br /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> <asp:Button ID="Button1" runat="server" Text="Button" /> </div> </form> <script> grecaptcha.ready(function() { grecaptcha.execute('SITE_KEY_HERE', {action: 'homepage'}).then(function(token) { document.getElementById("g-recaptcha-response").value = token; }); }); </script> </body> </html>

vb code

Imports System.Web.Script.Serialization Dim url As String Dim secretKey As String = "SECRET_KEY_HERE" Dim recaptcha_response = Request.Form("g-recaptcha-response") Label1.Text = "" url = String.Format("https://www.google.com/recaptcha/api/siteverify?secret=" & secretKey & "&response=" & recaptcha_response) Dim client As New Net.WebClient() Dim stream As IO.Stream = client.OpenRead(url) Dim reader As New IO.StreamReader(stream) Dim jsonData As String = reader.ReadToEnd Dim JsonResults As Object = New JavaScriptSerializer().Deserialize(Of Object)(jsonData) If JsonResults("success") = True Then 'Submit request 'For testing only '---------------------------------------------------------------------- Label1.Text = "<br/>" Label1.Text &= "success: " & JsonResults("success") & "<br/>" Label1.Text &= "score: " & JsonResults("score") & "<br/>" Label1.Text &= "action: " & JsonResults("action") & "<br/>" Label1.Text &= "challenge_ts: " & JsonResults("challenge_ts") & "<br/>" '---------------------------------------------------------------------- Else 'Stop submit 'For testing only '---------------------------------------------------------------------- Label1.Text &= "success: " & JsonResults("success") & "<br/>" Label1.Text &= "errorcodes: " & JsonResults("error-codes") & "<br/>" 'For testing only '---------------------------------------------------------------------- End If reader.Close()


So did you have any trouble figuring it out base on the information google provide, or did you have to do your own research as well. Let me know!