【发布时间】:2014-12-16 23:27:45
【问题描述】:
我正在尝试让新的Google reCaptcha 在我的 ASP.NET 项目中工作,但我无法让它成为新的“我不是机器人”。
我在那里有旧的,在对 developers.google.com 网站进行了大量研究之后,一切看起来都一样(他们甚至指向我下载相同的 dll - 1.0.5)。所以,我得到了新密钥并将它们放入,它可以工作,但它看起来就像旧的 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 找到了另一种方法。
【问题讨论】:
-
第二个答案对我有用。如果其中任何一个对您有用,那么您可能希望选择一个正确的,以便未来的人们可以知道要使用哪种解决方案。只是一个建议:)