نوجيت
Install-Package Spaier.Recaptcha
صافي سطر الأوامر
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
والمستندات الرسمية
معهد ماساتشوستس للتكنولوجيا