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!