首页 > 解决方案 > C# ASP.NET 中的 reCAPTCHA v3

问题描述

我正在尝试让 Google reCAPTCHA v3 在 ASP.NET WebForm 上运行。我使用的库是 BitArmory.ReCaptcha ( https://github.com/BitArmory/ReCaptcha ) 但示例代码无法编译:

//1. Get the client IP address in your chosen web framework
string clientIp = this.HttpContext.Connection.RemoteIpAddress.ToString();
string token = null;
string secret = "your_secret_key";

//2. Extract the `#captcha` field from the hidden HTML form in your chosen 
web framework
if( this.Request.Form.TryGetValue("captcha", out var formField) )
{
    token = formField;
} 

//3. Validate the reCAPTCHA with Google
var captchaApi = new ReCaptchaService();
var result = await captchaApi.Verify3Async(token, clientIp, secret);

if( !result.IsSuccess || result.Action != "SOME_ACTION" || result.Score < 0.5 )
{
    // The POST is not valid
    return new BadRequestResult();
}
else{
    //continue processing, everything is okay!
}

我在 this.HttpContext.Connection.RemoteIpAddress、this.Request.Form.TryGetValue 和 result.IsSuccess 上遇到错误(在 result.* 上等等)。该站点正在运行 .NET 4.8。这适用于 .NET Framework,还是仅适用于 .NET Core?

谢谢。

标签: c#asp.net.netrecaptcha-v3

解决方案


推荐阅读