新谷歌的Recaptcha与ASP.Net [英] New Google Recaptcha with ASP.Net
问题描述
我试图获得新的谷歌的reCAPTCHA 的在我的ASP工作。 NET项目,我有得到它的问题是新的我不是机器人。
I am attempting to get the new Google reCaptcha working in my ASP.NET project and I am having problems getting it to be the new one "I'm not a robot".
我有旧的在那里并在developers.google.com网站做大量的研究后,一切看起来都一样(他们甚至点我下载同一个DLL的 - 1.0.5)。所以,我得到了新的钥匙,把他们在和它的作品,但它看起来就像老验证码。
I had the old one in there and after doing much research on the developers.google.com web site, everything looks the same (they even point me to a download of the same dll - 1.0.5). So, I got the new keys and put them in and it works but it looks just like the old reCaptcha.
有没有人得到了新的与他们的ASP.Net工作?我缺少什么?
Has anyone gotten the new one to work with their ASP.Net? What am I missing?
编辑:
所以,玩弄在测试应用程序和搜索我发现了一些其他网站,如果我创建了一个页面是这样的:
So playing around in a test app and searching some other web sites I found that if I create a page like this:
<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>
然后在我的code-后面(的button1_Click),我这样做:
And then in my code-behind (Button1_Click), I do this:
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
要么是空的或填充取决于他们是否是一个机器人或没有。问题是,我现在需要借此回应并把它与我的私钥,以谷歌,所以我可以确认的响应不是由机器人提供的,在我的code-落后,但我无法弄清楚如何。我想这(代替成功= TRUE
)
The recaptchaResponse
will either be empty or filled in depending on if they are a bot or not. The issue is, I now need to take this response and send it to google with my private key so I can verify that the response was not provided by a bot, in my code-behind, but I cannot figure out how. I tried this (in place of 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响应,并试图这个代替:
But, I could not figure out how to get the async stuff working and I cannot find anything on what ReCaptchaModel
is, so I found another way to call a web service and get a json response and tried this instead:
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 。不是我想要的。该谷歌网页是不是非常有用,我被困在哪里去了。我需要一些帮助,可致电谷歌验证服务或是否有人已经发现了另一个办法从ASP.NET做到这一点。
But, this just gives me the content of the web page at https://www.google.com/recaptcha. Not what I want. The Google page isn't very useful and I am stuck on where to go. I need some help either calling the Google verify service or if anyone has found another way to do this from ASP.NET.
推荐答案
我只是放弃了,当我遇到一些不相关的,让我再想想,并以不同的方式运行。在我上面的最后一次尝试,我试图通过私钥和验证码响应其中的数据,所以我想它在的WebRequest的创建
和它的工作。下面是最终的解决方案:
I had just about given up when I ran across something unrelated that made me think about it again and in a different way. In my last attempt above, I was attempting to pass the private key and recaptcha response as the data, so I tried it in the create
of the WebRequest and it worked. Here is the final solution:
使用上面贴了相同的HTML,我创建了可以在按钮单击事件,我检查Page.IsValid打电话,调用该函数的函数:
Using the same HTML posted above, I created a function that I can call in the button click event where I check the Page.IsValid and call this function:
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
我敢肯定有到code进行了改进,但它的作品。我看不出加入一些JSON库参考阅读一件事,我只是检查字符串。
I'm sure there are improvements to be made to the code, but it works. I couldn't see adding references to some JSON libraries for reading one thing I just check the string.
这篇关于新谷歌的Recaptcha与ASP.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!