นูเก็ต
Install-Package Spaier.Recaptcha
.NET CLI
dotnet add package Spaier.Recaptcha
public void ConfigureServices ( IServiceCollection services ) {
// Your Code
services . AddRecaptcha ( )
// Use appsettings.json
//.AddInMemoryConfigurationStore(Configuration.GetSection("Recaptcha"))
. AddInMemoryConfigurationStore ( new Dictionary < string , RecaptchaConfiguration >
{
[ "Sitekey1" ] = new RecaptchaConfiguration ( RecaptchaDefaults . TestSecretKey , RecaptchaSecretType . V2 ) ,
[ "Sitekey2" ] = new RecaptchaConfiguration ( RecaptchaDefaults . TestSecretKey , RecaptchaSecretType . V2Android ) ,
[ "Sitekey3" ] = new RecaptchaConfiguration ( RecaptchaDefaults . TestSecretKey , RecaptchaSecretType . V3 ) ,
} )
. AddTokenHeaderProvider ( )
. AddConfigurationHeaderProvider ( )
. AddRecaptchaHttpClient ( configureHttpBuilder : httpBuilder =>
{
// You can setup Polly here
httpBuilder . AddTransientHttpErrorPolicy ( builder => builder . WaitAndRetryAsync ( new [ ]
{
TimeSpan . FromSeconds ( 1 ) ,
TimeSpan . FromSeconds ( 5 ) ,
TimeSpan . FromSeconds ( 10 )
} ) ) ;
} )
. UseGoogleUrl ( ) ;
// UseGlobalUrl(); // will use recaptcha.net mirror. Useful for countries where google.com is blocked.
// UseCustomUrl("your_url"); // will use custom url for validation.
}ValidateRecaptcha กับการดำเนินการ Configurations กำหนดค่าที่อนุญาตสำหรับการดำเนินการ หากไม่มีการระบุ คุณสามารถใช้การกำหนดค่าใดก็ได้ หากมีการระบุเพียงรายการเดียว คุณสามารถละเว้นโทเค็นการกำหนดค่าได้
AllowedAction ทำงานร่วมกับ V2 หรือ V3 ไม่ต้องระบุให้ข้ามการตรวจสอบการดำเนินการ
MinimumScore ทำงานร่วมกับ V3 ค่าเริ่มต้นเป็น 0.5
UseModelErrors กำหนดว่าจะเพิ่มข้อผิดพลาดลงในโมเดล MVC หรือไม่ จริงตามค่าเริ่มต้น
คุณสามารถส่งการตอบสนอง recaptcha ไปยังการดำเนินการได้โดยใช้ FromRecaptchaResponseAttribute พร้อมด้วยพารามิเตอร์เมธอดใดๆ ที่ได้รับจาก IRecaptchaResponse
[ HttpPost ]
[ AllowAnonymous ]
[ ValidateRecaptcha ( Configurations = new [ ] { "Sitekey1" , "Sitekey2" } , MinimumScore = 0.7 , AllowedAction = "register" ) ]
public async Task < ActionResult > ProtectedByV3AndV2 ( [ FromRecaptchaResponse ] RecaptchaResponse response )
{
// Your Code
} ควรส่งการตอบสนองของ reCAPTCHA ในส่วนหัว HTTP ด้วยคีย์ที่ระบุหรือ g-recaptcha-response
ควรส่งคีย์การกำหนดค่าของ reCAPTCHA ในส่วนหัว HTTP พร้อมด้วยคีย์ที่ระบุหรือ g-recaptcha-type หากมีการกำหนดค่ามากกว่าหนึ่งรายการในร้านค้าหรือระบุไว้ใน Configurations
หากโทเค็นที่ส่งผ่านฝั่งไคลเอ็นต์เป็นแบบจำลองที่ไม่ถูกต้อง ข้อผิดพลาดจะถูกเพิ่มใน ModelState ดู RecaptchaErrorCodes , ValidateRecaptchaAttribute.ErrorCodes และเอกสารอย่างเป็นทางการ
เอ็มไอที