Spaier.Recaptcha
3.2.0
努盖特
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 模型中。默认为真。
您可以将FromRecaptchaResponseAttribute
与从IRecaptchaResponse
派生的任何方法参数结合使用,将 recaptcha 响应传递给操作。
[ HttpPost ]
[ AllowAnonymous ]
[ ValidateRecaptcha ( Configurations = new [ ] { "Sitekey1" , "Sitekey2" } , MinimumScore = 0.7 , AllowedAction = "register" ) ]
public async Task < ActionResult > ProtectedByV3AndV2 ( [ FromRecaptchaResponse ] RecaptchaResponse response )
{
// Your Code
}
reCAPTCHA 的响应应使用指定的密钥或g-recaptcha-response
在 HTTP 标头中传递。
如果存储中或在Configurations
中指定了多个配置,则 reCAPTCHA 的配置密钥应在具有指定密钥或g-recaptcha-type
HTTP 标头中传递。
如果客户端传递的令牌无效,模型错误将添加到ModelState
中。请参阅RecaptchaErrorCodes
、 ValidateRecaptchaAttribute.ErrorCodes
和官方文档
麻省理工学院