Novo Google Recaptcha com ASP.Net
Estou tentando obter o novoGoogle reCaptcha trabalhando no meu projeto ASP.NET e estou tendo problemas para que seja o novo "Eu não sou um robô".
Eu tinha o antigo lá e depois de fazer muita pesquisa no site developers.google.com, tudo parece o mesmo (eles até me indicam um download da mesma dll - 1.0.5). Então, eu peguei as novas chaves e as coloquei, e funciona, mas parece com o antigo reCaptcha.
Alguém conseguiu que o novo funcionasse com o ASP.Net? o que estou perdendo?
EDITAR:
Então, brincando em um aplicativo de teste e pesquisando em outros sites, descobri que, se eu criar uma página como esta:
<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>
E então no meu code-behind (Button1_Click), faço isso:
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
orecaptchaResponse
ficará vazio ou preenchido, dependendo se é um bot ou não. O problema é que agora preciso pegar essa resposta e enviá-la ao Google com minha chave privada para que eu possa verificar se a resposta não foi fornecida por um bot, no meu code-behind, mas não consigo descobrir como. Eu tentei isso (no lugar deSuccess = 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
Mas não consegui descobrir como fazer o trabalho assíncrono e não consigo encontrar nada sobre o queReCaptchaModel
é, então eu encontrei outra maneira de chamar um serviço web e obter uma resposta json e tentei:
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
Mas, isso apenas fornece o conteúdo da página da web emhttps://www.google.com/recaptcha. Não é o que eu quero. oPágina do Google não é muito útil e estou preso aonde ir. Preciso de ajuda para ligar para o serviço de verificação do Google ou se alguém encontrou outra maneira de fazer isso no ASP.NET.