我正在尝试获取新的 Google reCaptcha在我的 ASP.NET 项目中工作,我在使其成为新项目“我不是机器人”时遇到问题。
我在那里有旧的,在对developers.google.com网站进行了大量研究后,一切看起来都一样(他们甚至向我指出了相同dll的下载 - 1.0.5)。因此,我拿到了新 key 并将其放入,它可以工作,但它看起来就像旧的 reCaptcha。
有人已经获得了可以与他们的 ASP.Net 一起使用的新版本吗?我错过了什么?
编辑:
因此,在测试应用程序中进行测试并搜索其他一些网站时,我发现如果我创建这样的页面:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>reCAPTCHA demo: Simple page</title>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<form id="form1" runat="server" action="?" method="POST">
<div>
<div class="g-recaptcha" data-sitekey="My Public Key"></div>
<br/>
<asp:Button ID="Button1" runat="server" Text="Submit" />
</div>
</form>
</body>
</html>
然后在我的代码隐藏(Button1_Click)中,我这样做:
Dim Success As Boolean
Dim recaptchaResponse As String = request.Form("g-recaptcha-response")
If Not String.IsNullOrEmpty(recaptchaResponse) Then
Success = True
Else
Success = False
End If
recaptchaResponse
要么为空,要么被填充,具体取决于它们是否是机器人。问题是,我现在需要获取此响应并使用我的私钥将其发送给谷歌,以便我可以在我的代码隐藏中验证该响应不是由机器人提供的,但我不知道如何实现。我尝试了这个(代替 Success = True
):
Dim client As New System.Net.Http.HttpClient()
client.BaseAddress = New Uri("https://www.google.com/recaptcha/")
client.DefaultRequestHeaders.Accept.Clear()
client.DefaultRequestHeaders.Accept.Add(New Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"))
Dim response As Net.Http.HttpResponseMessage = Await client.GetAsync("api/siteverify?secret=My Private key&response=" + recaptchaResponse)
If (response.IsSuccessStatusCode) Then
Dim CaptchResponse As ReCaptchaModel = Await response.Content.ReadAsAsync(Of ReCaptchaModel)()
Success = CaptchResponse.success
Else
Success = False
End If
但是,我不知道如何让异步的东西工作,而且我找不到任何关于 ReCaptchaModel
的内容,所以我找到了另一种方法来调用 Web 服务并获取 json 响应,尝试这样做:
Dim request As Net.WebRequest = Net.WebRequest.Create("https://www.google.com/recaptcha/")
Dim Data As String = "api/siteverify?secret=My Private Key&response=" + recaptchaResponse
request.Method = "POST"
request.ContentType = "application/json; charset=utf-8"
Dim postData As String = "{""data"":""" + Data + """}"
'get a reference to the request-stream, and write the postData to it
Using s As IO.Stream = request.GetRequestStream()
Using sw As New IO.StreamWriter(s)
sw.Write(postData)
End Using
End Using
'get response-stream, and use a streamReader to read the content
Using s As IO.Stream = request.GetResponse().GetResponseStream()
Using sr As New IO.StreamReader(s)
'decode jsonData with javascript serializer
Dim jsonData = sr.ReadToEnd()
Stop
End Using
End Using
但是,这只是给我 https://www.google.com/recaptcha 网页的内容。不是我想要的。 Google page不是很有用,我不知道该去哪里。我需要一些帮助,要么调用 Google 验证服务,要么有人找到其他方法从 ASP.NET 执行此操作。
最佳答案
当我遇到一些不相关的事情时,我正要放弃,这让我以不同的方式再次思考它。在上面的最后一次尝试中,我尝试将私钥和验证码响应作为数据传递,因此我在 WebRequest 的 create
中进行了尝试,并且成功了。最终解决方案如下:
使用上面发布的相同 HTML,我创建了一个可以在按钮单击事件中调用的函数,在该事件中我检查 Page.IsValid 并调用此函数:
Private Function IsGoogleCaptchaValid() As Boolean
Try
Dim recaptchaResponse As String = Request.Form("g-recaptcha-response")
If Not String.IsNullOrEmpty(recaptchaResponse) Then
Dim request As Net.WebRequest = Net.WebRequest.Create("https://www.google.com/recaptcha/api/siteverify?secret=My Private Key&response=" + recaptchaResponse)
request.Method = "POST"
request.ContentType = "application/json; charset=utf-8"
Dim postData As String = ""
'get a reference to the request-stream, and write the postData to it
Using s As IO.Stream = request.GetRequestStream()
Using sw As New IO.StreamWriter(s)
sw.Write(postData)
End Using
End Using
''get response-stream, and use a streamReader to read the content
Using s As IO.Stream = request.GetResponse().GetResponseStream()
Using sr As New IO.StreamReader(s)
'decode jsonData with javascript serializer
Dim jsonData = sr.ReadToEnd()
If jsonData = "{" & vbLf & " ""success"": true" & vbLf & "}" Then
Return True
End If
End Using
End Using
End If
Catch ex As Exception
'Dont show the error
End Try
Return False
End Function
我确信代码还需要改进,但它确实有效。我看不到添加对某些 JSON 库的引用来读取我只是检查字符串的一件事。
关于asp.net - 带有 ASP.Net 的新 Google Recaptcha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27515838/