ReCaptcha Asp Net
Version 1.4
ASP Net의 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] " />옵션 옵션 옵트 차의 기본 언어를 변경하려면 (recaptcha 사이트에서 언어 코드를 얻으려면 : https://developers.google.com/recaptcha/docs/language) "auto"를 사용할 수 있습니다.
< add key = " recaptcha-language-key " value = " [language-code] " />또는 C# 코드를 통해 : 한 번만 호출해야합니다.
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 ) ;선택 사항 기본 테마를 밝은 모드에서 어두운 모드로 변경하려면 선택 사항입니다.
< 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 ) ;
.. .비밀 키가 유효하지 않거나 유효하지 않은 사용자 응답을 validatecaptcha 매개 변수로 전달하는 경우 다음과 같은 예외를 던질 수 있습니다.
throw new ReCaptchaException ( ) ;비동기라고도 할 수도 있습니다.
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" ) ;
}
}