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
アクションに対して許可される構成を定義します。何も指定されていない場合は、任意の構成を使用できます。 1 つだけ指定した場合は、構成トークンを省略できます。
AllowedAction
V2 または V3 で動作します。動作チェックをスキップする場合は指定しないでください。
MinimumScore
V3 で動作します。デフォルトは0.5
です。
UseModelErrors
MVC モデルにエラーを追加するかどうかを決定します。デフォルトでは true。
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 の応答は、指定されたキーまたはg-recaptcha-response
を含む HTTP ヘッダーで渡される必要があります。
reCAPTCHA の構成キーは、ストアに複数の構成がある場合、またはConfigurations
で指定されている場合は、指定されたキーまたはg-recaptcha-type
を使用して HTTP ヘッダーで渡す必要があります。
クライアント側から渡されたトークンが無効な場合、モデルエラーがModelState
に追加されます。 RecaptchaErrorCodes
、 ValidateRecaptchaAttribute.ErrorCodes
および公式ドキュメントを参照してください。
マサチューセッツ工科大学