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
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 = " "
Label1.Text &= "success: " & JsonResults("success") & " "
Label1.Text &= "score: " & JsonResults("score") & " "
Label1.Text &= "action: " & JsonResults("action") & " "
Label1.Text &= "challenge_ts: " & JsonResults("challenge_ts") & " "
'----------------------------------------------------------------------
Else
'Stop submit
'For testing only
'----------------------------------------------------------------------
Label1.Text &= "success: " & JsonResults("success") & " "
Label1.Text &= "errorcodes: " & JsonResults("error-codes")(0) & " "
'----------------------------------------------------------------------
End If
End Using
' Clean up the response.
response.Close()
End Sub
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
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 = " "
Label1.Text &= "success: " & JsonResults("success") & " "
Label1.Text &= "score: " & JsonResults("score") & " "
Label1.Text &= "action: " & JsonResults("action") & " "
Label1.Text &= "challenge_ts: " & JsonResults("challenge_ts") & " "
'----------------------------------------------------------------------
Else
'Stop submit
'For testing only
'----------------------------------------------------------------------
Label1.Text &= "success: " & JsonResults("success") & " "
Label1.Text &= "errorcodes: " & JsonResults("error-codes") & " "
'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!