ReCaptcha Asp Net
Version 1.4
ASPネットのGoogle Recaptcha、簡素化
nuget url:https://www.nuget.org/packages/recaptcha-aspnet
PM > Install-Package ReCaptcha-AspNet
https://www.google.com/recaptcha/adminで秘密と公開の鍵を入手してください
内部にapp/web.configを追加します
< add key = " recaptcha-secret-key " value = " ...[secret key] " />
< add key = " recaptcha-public-key " value = " ...[public-key] " />オプションCaptchaのデフォルト言語を変更する場合(Recaptchaサイトで言語コードを取得:https://developers.google.com/recaptcha/docs/language)、またはvalue "auto"を使用できます。
< add key = " recaptcha-language-key " value = " [language-code] " />またはC#コードを介して:それを一度呼び出す必要があります、これを置くのに適した場所はapplication_start()関数です
string publicKey = "...[public-key]" ;
string secretKey = "...[secret-key]" ;
ReCaptcha . Configure ( publicKey , secretKey ) ;
// Optional, select a default language:
ReCaptchaLanguage defaultLanguage = ReCaptchaLanguage . German ;
ReCaptcha . Configure ( publicKey , secretKey , defaultLanguage ) ;
//Auto-select language from System.Thread.CurrentCulture
ReCaptchaLanguage defaultLanguage = ReCaptchaLanguage . Auto ;
ReCaptcha . Configure ( publicKey , secretKey , defaultLanguage ) ;デフォルトのテーマをLightからDarkモードに変更する場合はオプション。
< add key = " recaptcha-language-theme " value = " dark " />またはC#コードを介して:
//Optional select a default dark theme
ReCaptchaLanguage defaultLanguage = ReCaptchaLanguage . Auto ;
ReCaptcha . Configure ( publicKey , secretKey , defaultLanguage , ReCaptchaTheme . dark ) ; あなたのフォームの内側
< form action =" myAction " >
< input type =" text " name =" myinput1 " />
@ReCaptcha.GetCaptcha() <!-- Will show your captcha as your configured Language,
if no language is defined it will show ReCaptcha default language (English) -->
</ form >設定されたデフォルト言語をオーバーライドする場合はオプション:
< form action =" myAction " >
< input type =" text " name =" myinput1 " />
@ReCaptcha.GetCaptcha(ReCaptchaLanguage.PortugueseBrazil) <!-- Will show your ReCaptcha as Portuguese,
overriding any previous configuration -->
</ form >設定されたテーマをオーバーライドする場合はオプション(Light/Dark):
< form action =" myAction " >
< input type =" text " name =" myinput1 " />
@ReCaptcha.GetCaptcha(theme: ReCaptchaTheme.dark) <!-- Will show your ReCaptcha on dark theme,
overriding any previous configuration -->
</ form >あなたのフォームの内側
< script type =" text/javascript " > function submit ( ) { $ ( 'form' ) . submit ( ) ; } </ script >
< form action =" myAction " >
< input type =" text " name =" myinput1 " />
@ReCaptcha.GetInvisibleCaptcha("submit", "Save") <!-- Will show a button, with a Label Save and call function "submit();" after user click ok and pass Captcha -->
</ form >設定されたデフォルト言語をオーバーライドする場合はオプション:
< script type =" text/javascript " > function submit ( ) { $ ( 'form' ) . submit ( ) ; } </ script >
< form action =" myAction " >
< input type =" text " name =" myinput1 " />
@ReCaptcha.GetInvisibleCaptcha("submit", "Save", ReCaptchaLanguage.PortugueseBrazil) <!-- Will show your Invisible ReCaptcha as Portuguese, overriding any previous configuration -->
</ form >コントローラー機能内またはフィルター内
string userResponse = HttpContext . Request . Params [ "g-recaptcha-response" ] ;
bool validCaptcha = ReCaptcha . ValidateCaptcha ( userResponse ) ;
if ( validCaptcha ) {
// Real User, validated !
DoStuff ( ) ;
.. .
} else {
// Bot Attack, non validated !
return RedirectToAction ( "YouAreARobot" , "Index" ) ;
} プロキシを使用して、recaptchaサーバーにユーザーの応答を送信できます
.. .
const string proxyIp = "xxx.xxx.xxx.xxx" ;
const int proxyPort = 1234 ;
WebProxy webProxy = new WebProxy ( proxyIp , proxyPort ) ;
bool validCaptcha = ReCaptcha . ValidateCaptcha ( userResponse , webProxy ) ;
.. .シークレットキーが無効である場合、またはhevalatecaptchaパラメーターとして無効なユーザー応答を渡す場合、次の例外をスローする場合があります。
throw new ReCaptchaException ( ) ;また、asyncと呼ぶこともできます。
public async ActionResult MyFunction ( ) {
string userResponse = HttpContext . Request . Params [ "g-recaptcha-response" ] ;
var validCaptcha = ReCaptcha . ValidateCaptchaAsync ( userResponse ) ;
DoSomeParallelStuff ( ) ;
if ( await validCaptcha ) {
// Real User, validated !
DoStuff ( ) ;
.. .
} else {
// Bot Attack, non validated !
return RedirectToAction ( "YouAreARobot" , "Index" ) ;
}
}