
ไลบรารีสำหรับการตรวจสอบฝั่งเซิร์ฟเวอร์ของ Google Recaptcha V2/V3 Token สำหรับ ASP.NET
recaptcha.verify.net เริ่มต้นจากเวอร์ชัน 2.0.0 รองรับแพลตฟอร์มต่อไปนี้และเป้าหมายใด ๆ ที่รองรับมาตรฐาน. NET จาก 2.0:
แพ็คเกจสามารถติดตั้งได้โดยใช้ Visual Studio UI (เครื่องมือ> Nuget Package Manager> จัดการแพ็คเกจ NUGET สำหรับโซลูชันและค้นหา "Recaptcha.Verify.net")
สามารถติดตั้งแพ็คเกจเวอร์ชันล่าสุดได้โดยใช้ Package Manager Console:
PM> Install-Package Recaptcha.Verify.Net
{
"Recaptcha" : {
"SecretKey" : " <recaptcha secret key> " ,
"ScoreThreshold" : 0.5
}
} public void ConfigureServices ( IServiceCollection services )
{
services . AddRecaptcha ( Configuration . GetSection ( "Recaptcha" ) ) ;
//...
} [ ApiController ]
[ Route ( "api/[controller]" ) ]
public class LoginController : Controller
{
private const string _loginAction = "login" ;
private readonly ILogger _logger ;
private readonly IRecaptchaService _recaptchaService ;
public LoginController ( ILoggerFactory loggerFactory , IRecaptchaService recaptchaService )
{
_logger = loggerFactory . CreateLogger < LoginController > ( ) ;
_recaptchaService = recaptchaService ;
}
[ HttpPost ]
public async Task < IActionResult > Login ( [ FromBody ] Credentials credentials , CancellationToken cancellationToken )
{
var checkResult = await _recaptchaService . VerifyAndCheckAsync (
credentials . RecaptchaToken ,
_loginAction ,
cancellationToken ) ;
if ( ! checkResult . Success )
{
if ( ! checkResult . Response . Success )
{
// Handle unsuccessful verification response
_logger . LogError ( "Recaptcha error: {errorCodes}" , JsonConvert . SerializeObject ( checkResult . Response . ErrorCodes ) ) ;
}
if ( ! checkResult . ScoreSatisfies )
{
// Handle score less than specified threshold for v3
}
// Unsuccessful verification and check
return BadRequest ( ) ;
}
// Process login
return Ok ( ) ;
}
}{
"Recaptcha" : {
...
"AttributeOptions" : {
"ResponseTokenNameInHeader" : " RecaptchaTokenInHeader " , // If token is passed in header
"ResponseTokenNameInQuery" : " RecaptchaTokenInQuery " , // If token is passed in query
"ResponseTokenNameInForm" : " RecaptchaTokenInForm " // If token is passed in form
}
}
}หรือตั้งค่าในการเริ่มต้น getResponSetokenfromactionArguments หรือ getResponSetokenFromeXecutingContext ตัวแทนที่ชี้ให้เห็นวิธีการรับโทเค็นจากข้อมูลที่แยกวิเคราะห์
services . AddRecaptcha ( Configuration . GetSection ( "Recaptcha" ) ,
// Specify how to get token from parsed arguments for using in RecaptchaAttribute
o => o . AttributeOptions . GetResponseTokenFromActionArguments =
d =>
{
if ( d . TryGetValue ( "credentials" , out var credentials ) )
{
return ( ( BaseRecaptchaCredentials ) credentials ) . RecaptchaToken ;
}
return null ;
} ) ;โมเดลข้อมูลรับรองที่ใช้ในตัวอย่างมีคลาสพื้นฐานที่มีคุณสมบัติที่มีโทเค็น
public class BaseRecaptchaCredentials
{
public string RecaptchaToken { get ; set ; }
}
public class Credentials : BaseRecaptchaCredentials
{
public string Login { get ; set ; }
public string Password { get ; set ; }
} [ Recaptcha ( "login" ) ]
[ HttpPost ( "Login" ) ]
public async Task < IActionResult > Login ( [ FromBody ] Credentials credentials , CancellationToken cancellationToken )
{
// Process login
return Ok ( ) ;
}เกณฑ์คะแนนใน appsettings.json เป็นตัวเลือกและค่าสามารถส่งผ่านไปยังฟังก์ชัน VerifyandCheckasync โดยตรง
var scoreThreshold = 0.5f ;
var checkResult = await _recaptchaService . VerifyAndCheckAsync (
credentials . RecaptchaToken ,
_loginAction ,
scoreThreshold ) ;จากคะแนนคุณสามารถดำเนินการแปรผันในบริบทของเว็บไซต์ของคุณแทนที่จะปิดกั้นการรับส่งข้อมูลเพื่อปกป้องเว็บไซต์ของคุณได้ดีขึ้น เกณฑ์คะแนนที่ระบุไว้สำหรับการกระทำช่วยให้คุณบรรลุการวิเคราะห์ความเสี่ยงแบบปรับตัวและการป้องกันตามบริบทของการกระทำ
{
"Recaptcha" : {
"SecretKey" : " <recaptcha secret key> " ,
"ScoreThreshold" : 0.5 ,
"ActionsScoreThresholds" : {
"login" : 0.75 ,
"test" : 0.9
}
}
} // Response will be checked with score threshold equal to 0.75
var checkResultLogin = await _recaptchaService . VerifyAndCheckAsync ( credentials . RecaptchaToken , "login" ) ;
// Response will be checked with score threshold equal to 0.9
var checkResultTest = await _recaptchaService . VerifyAndCheckAsync ( credentials . RecaptchaToken , "test" ) ;
// Response will be checked with score threshold equal to 0.5
var checkResultSignUp = await _recaptchaService . VerifyAndCheckAsync ( credentials . RecaptchaToken , "signup" ) ;หากการตรวจสอบการตอบสนองการตรวจสอบจะต้องเสร็จสิ้นแยกกันคุณสามารถใช้ Verifyasync แทน VerifyandCheckasync
var response = await _recaptchaService . VerifyAsync ( credentials . RecaptchaToken ) ;ห้องสมุดสามารถสร้างข้อยกเว้นต่อไปนี้
| ข้อยกเว้น | คำอธิบาย |
|---|---|
| emportactionException | ข้อยกเว้นนี้จะถูกโยนลงเมื่อการกระทำที่ส่งผ่านในฟังก์ชั่นว่างเปล่า |
| empycaptchaanswerexception | ข้อยกเว้นนี้จะถูกโยนลงเมื่อคำตอบ Captcha ผ่านฟังก์ชั่นว่างเปล่า |
| httprequestException | ข้อยกเว้นนี้จะถูกโยนลงเมื่อคำขอ HTTP ล้มเหลว ร้านค้า refit.apiexception เป็นข้อยกเว้นภายใน |
| MinscorenotspecifiedException | ข้อยกเว้นนี้จะถูกโยนลงเมื่อไม่ได้ระบุคะแนนขั้นต่ำและคำขอมีค่าคะแนน (ใช้ V3 recaptcha) |
| SecretKeyNotSpecifiedException | ข้อยกเว้นนี้จะถูกโยนลงเมื่อไม่ได้ระบุรหัสลับในตัวเลือกหรือขอพารามิเตอร์ |
| UnknownerrorkeyException | ข้อยกเว้นนี้ถูกโยนทิ้งเมื่อไม่ทราบคีย์ข้อผิดพลาดการตอบสนองการตรวจสอบ |
ข้อยกเว้นทั้งหมดเหล่านี้สืบทอดมาจาก RecaptchaserviceException
ตัวอย่างสามารถพบได้ในที่เก็บไลบรารี: