ReCaptcha Asp Net
Version 1.4
Google recaptcha用于ASP网络,简化
Nuget URL:https://www.nuget.org/packages/recaptcha-aspnet
PM > Install-Package ReCaptcha-AspNet
在https://www.google.com/recaptcha/admin上获取您的秘密和公共密钥
添加到您的应用程序/web.config内部
< add key = " recaptcha-secret-key " value = " ...[secret key] " />
< add key = " recaptcha-public-key " value = " ...[public-key] " />Optional if you want to alter the default language of the Captcha (Get the language code on ReCaptcha site: https://developers.google.com/recaptcha/docs/language) Or you can use value "Auto" and it will get the Language from System.Thread.CurrentCulture
< 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 ) ;如果您想将默认主题从光模式更改为黑暗模式,请进行可选。
< 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 >如果您想覆盖已配置的主题(浅色/黑暗),请进行可选:
< 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 ) ;
.. .如果秘密密钥无效,则可能会引发以下异常,或者您将无效的用户响应作为vasteratecaptcha参数:
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" ) ;
}
}