Nuget
Install-Package Spaier.Recaptcha
CLI de .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
a una acción. Configurations
define las configuraciones permitidas para una acción. Si no se especifica ninguno, puede utilizar cualquier configuración. Si solo se especifica uno, puede omitir el token de configuración.
AllowedAction
funciona con V2 o V3. No especifique omitir la verificación de acción.
MinimumScore
funciona con V3. El valor predeterminado es 0.5
.
UseModelErrors
determina si se agregarán errores al modelo MVC. Verdadero por defecto.
Puede pasar una respuesta recaptcha a una acción utilizando FromRecaptchaResponseAttribute
con cualquier parámetro de método derivado 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 respuesta de un reCAPTCHA debe pasarse en un encabezado HTTP con la clave especificada o g-recaptcha-response
.
La clave de configuración de un reCAPTCHA debe pasarse en un encabezado HTTP con la clave especificada o g-recaptcha-type
si hay más de una configuración almacenada o especificada en Configurations
.
Si el token pasado por el lado del cliente no es válido, se agregarán errores de modelo a ModelState
. Consulte RecaptchaErrorCodes
, ValidateRecaptchaAttribute.ErrorCodes
y documentos oficiales.
MIT