Nuget
Install-Package Spaier.Recaptcha
CLI .NET
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
à une action. Configurations
définissent les configurations autorisées pour une action. Si aucune n'est spécifiée, vous pouvez utiliser n'importe quelle configuration. Si un seul est spécifié, vous pouvez omettre le jeton de configuration.
AllowedAction
fonctionne avec la V2 ou la V3. Ne spécifiez pas d'ignorer la vérification des actions.
MinimumScore
fonctionne avec la V3. La valeur par défaut est 0.5
.
UseModelErrors
détermine si des erreurs seront ajoutées au modèle MVC. Vrai par défaut.
Vous pouvez transmettre une réponse recaptcha à une action en utilisant FromRecaptchaResponseAttribute
avec n'importe quel paramètre de méthode dérivé de IRecaptchaResponse
.
[ HttpPost ]
[ AllowAnonymous ]
[ ValidateRecaptcha ( Configurations = new [ ] { "Sitekey1" , "Sitekey2" } , MinimumScore = 0.7 , AllowedAction = "register" ) ]
public async Task < ActionResult > ProtectedByV3AndV2 ( [ FromRecaptchaResponse ] RecaptchaResponse response )
{
// Your Code
}
La réponse d'un reCAPTCHA doit être transmise dans un en-tête HTTP avec la clé spécifiée ou g-recaptcha-response
.
La clé de configuration d'un reCAPTCHA doit être transmise dans un en-tête HTTP avec la clé spécifiée ou g-recaptcha-type
s'il existe plusieurs configurations en magasin ou spécifiées dans Configurations
.
Si le jeton transmis côté client n'est pas valide, des erreurs de modèle seront ajoutées à ModelState
. Voir RecaptchaErrorCodes
, ValidateRecaptchaAttribute.ErrorCodes
et la documentation officielle
MIT