在ASP.NET中的服务器端验证的Recaptcha 2(无验证码CAPTCHA) [英] Validating Recaptcha 2 (No CAPTCHA reCAPTCHA) in ASP.NET's server side
问题描述
<一个href=\"http://googleonlinesecurity.blogspot.com/2014/12/are-you-robot-introducing-no-captcha.html\">The新的Recaptcha 2 看起来很有希望,但我没有找到一个方法来验证它在ASP.NET中的服务器端,
The new Recaptcha 2 looks promising, but i didn't find a way to validate it in ASP.NET's server side,
如果(Page.IsValid)
中的这个答案,适用于旧的Recaptcha,但不是新的,
if(Page.IsValid)
in This answer, is valid for the old Recaptcha, but not the new one,
如何验证新的验证码在服务器端?
How to validate the new reCAPTCHA in server side?
推荐答案
阅读的资源后,我结束了写这个类来处理的<一个验证href=\"http://googleonlinesecurity.blogspot.com/2014/12/are-you-robot-introducing-no-captcha.html\">the新的reCAPTCHA :
After reading many resources, I ended up with writing this class to handle the validation of the new ReCaptcha :
如前所述这里:当一个验证码是由最终用户,一个新的领域解决(G-recaptcha-响应)将在HTML中被填充。
As mentioned Here : When a reCAPTCHA is solved by end user, a new field (g-recaptcha-response) will be populated in HTML.
我们需要阅读此值,并把它传递给类下面来验证它:
We need to read this value and pass it to the class below to validate it:
在C#中:
在您的网页背后的code:
In the code behind of your page :
string EncodedResponse = Request.Form["g-Recaptcha-Response"];
bool IsCaptchaValid = (ReCaptchaClass.Validate(EncodedResponse) == "True" ? true : false);
if (IsCaptchaValid) {
//Valid Request
}
的类别:
using Newtonsoft.Json;
public class ReCaptchaClass
{
public static string Validate(string EncodedResponse)
{
var client = new System.Net.WebClient();
string PrivateKey = "6LcH-v8SerfgAPlLLffghrITSL9xM7XLrz8aeory";
var GoogleReply = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", PrivateKey, EncodedResponse));
var captchaResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<ReCaptchaClass>(GoogleReply);
return captchaResponse.Success;
}
[JsonProperty("success")]
public string Success
{
get { return m_Success; }
set { m_Success = value; }
}
private string m_Success;
[JsonProperty("error-codes")]
public List<string> ErrorCodes
{
get { return m_ErrorCodes; }
set { m_ErrorCodes = value; }
}
private List<string> m_ErrorCodes;
}
在VB.NET:
在您的网页背后的code:
In the code behind of your page :
Dim EncodedResponse As String = Request.Form("g-Recaptcha-Response")
Dim IsCaptchaValid As Boolean = IIf(ReCaptchaClass.Validate(EncodedResponse) = "True", True, False)
If IsCaptchaValid Then
'Valid Request
End If
的类别:
Imports Newtonsoft.Json
Public Class ReCaptchaClass
Public Shared Function Validate(ByVal EncodedResponse As String) As String
Dim client = New System.Net.WebClient()
Dim PrivateKey As String = "6dsfH-v8SerfgAPlLLffghrITSL9xM7XLrz8aeory"
Dim GoogleReply = client.DownloadString(String.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", PrivateKey, EncodedResponse))
Dim captchaResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(Of ReCaptchaClass)(GoogleReply)
Return captchaResponse.Success
End Function
<JsonProperty("success")> _
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
<JsonProperty("error-codes")> _
Public Property ErrorCodes() As List(Of String)
Get
Return m_ErrorCodes
End Get
Set(value As List(Of String))
m_ErrorCodes = value
End Set
End Property
Private m_ErrorCodes As List(Of String)
End Class
这篇关于在ASP.NET中的服务器端验证的Recaptcha 2(无验证码CAPTCHA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!