5

Having a hard time getting reCaptcha to validate on my site :(

I have tried to find other sources for VB.net implementations, but haven't had much luck. Here is what I have tried...

default.aspx.vb

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Web.Script.Serialization

Public Class _Default
Inherits System.Web.UI.Page

Sub reCaptcha_Click(ByVal sender As Object, ByVal e As EventArgs)
    If (capValidate()) Then
        MsgBox("Valid Recaptcha")
    Else
        MsgBox("Not Valid Recaptcha")
    End If

End Sub

Public Function capValidate() As Boolean
    Dim Response As String = Request("g-captcha-response")
    Dim Valid As Boolean = False
    Dim req As HttpWebRequest = DirectCast(WebRequest.Create(Convert.ToString("https://www.google.com/recaptcha/api/siteverify?secret=THIS IS WHERE MY KEY IS&response=") & Response), HttpWebRequest)

    Try
        Using wResponse As WebResponse = req.GetResponse()

            Using readStream As New StreamReader(wResponse.GetResponseStream())
                Dim jsonResponse As String = readStream.ReadToEnd()
                Dim js As New JavaScriptSerializer()
                Dim data As MyObject = js.Deserialize(Of MyObject)(jsonResponse)

                Valid = Convert.ToBoolean(data.success)
                Return Valid
            End Using
        End Using
    Catch ex As Exception
        Return False
    End Try
End Function

Public Class MyObject
    Public Property success() As String
        Get
            Return m_success
        End Get
        Set(value As String)
            m_success = Value
        End Set
    End Property
    Private m_success As String


End Class

And my front page...

<div class="g-recaptcha" 
data-sitekey="THIS IS WHERE MY SITE KEY IS"></div>
<asp:Button ID="btnLogin" CssClass="captcha_click" runat="server" Text="Check Recaptcha" OnClick="reCaptcha_Click"  TabIndex ="4"/>

My message boxes always return "not a valid recaptcha"

Can anyone shed some light on why I cannot get a valid recaptcha return?

Thanks!

5
  • If you remove the try/catch from your capValidate function do you get an exception thrown?
    – theduck
    May 28, 2015 at 19:47
  • No error, I checked that by adding a msgbox with a little error message, but it never got called. May 28, 2015 at 19:49
  • And does the jsonResponse always contain success: false? And is there an error-codes field?
    – theduck
    May 28, 2015 at 19:53
  • Hmm, just checked and it is always returning false. It gave me an error code of: "Missing-input-response" May 28, 2015 at 19:56
  • OK think your request is looking at the wrong field. See answer below.
    – theduck
    May 28, 2015 at 19:58

1 Answer 1

6

Try:

Dim Response As String = Request("g-recaptcha-response")

Note the re

2
  • :D no words. hahaha. All of this and it was a spelling mistake >.< May 28, 2015 at 20:02
  • I was able to use the OP's code 4 years later for my vb.net app. Thank you.
    – JustJohn
    Nov 30, 2019 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.