谷歌reCaptcha V2实施VB.net

时间:2015-05-28 18:56:16

标签: asp.net vb.net recaptcha

很难在我的网站上获得reCaptcha验证:(

我试图找到VB.net实现的其他来源,但是没有多少运气。这是我试过的......

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

我的头版......

<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"/>

我的留言框总是返回&#34;而不是有效的recaptcha&#34;

任何人都可以解释为什么我无法获得有效的重新回报?

谢谢!

1 个答案:

答案 0 :(得分:5)

尝试:

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

请注意re