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
和官方文檔
麻省理工學院