Yas*_*ith 6 .net c# recaptcha asp.net-web-api angularjs
我正在使用Angular js前端应用程序和Web API后端.我想在前端应用程序中使用google recaptcha.我可以通过引用以下URL来显示小部件.https://developers.google.com/recaptcha/docs/display.本文档不讨论使用密钥进行服务器端验证.但我注意到一些asp.net开发人员使用服务器端实现来验证带有密钥的google recaptcha响应.我是否需要使用密钥进行服务器端验证?与角度和Web API相关的最佳方法是什么?
是的,您需要进行服务器端验证,否则您未经身份验证的API端点不会受到不通过您网站的直接呼叫的保护.
解决方案非常直接,详情如下:https: //developers.google.com/recaptcha/docs/verify
但是一个非常简单的方法可以验证g-recaptcha-response网站给出的方法,如下所示:
public bool Validate(string encodedResponse)
{
if (string.IsNullOrEmpty(encodedResponse)) return false;
var secret = **your secret**;
if (string.IsNullOrEmpty(secret)) return false;
var client = new System.Net.WebClient();
var googleReply = client.DownloadString(
$"https://www.google.com/recaptcha/api/siteverify?secret={secret}&response={encodedResponse}");
return JsonConvert.DeserializeObject<RecaptchaResponse>(googleReply).Success;
}
Run Code Online (Sandbox Code Playgroud)
RecaptchaResponse是一个简单的类,我们看起来像这样:
public class RecaptchaResponse
{
[JsonProperty("success")]
public bool Success { get; set; }
[JsonProperty("error-codes")]
public IEnumerable<string> ErrorCodes { get; set; }
[JsonProperty("challenge_ts")]
public DateTime ChallengeTs { get; set; }
[JsonProperty("hostname")]
public string Hostname { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3050 次 |
| 最近记录: |