Google recaptcha pour ASP Net, simplifié
URL NUGET: https://www.nuget.org/packages/recaptcha-aspnet
PM > Install-Package ReCaptcha-AspNet
Obtenez vos clés secrètes et publiques sur https://www.google.com/recaptcha/admin
Ajoutez à votre application / web.config à l'intérieur
< add key = " recaptcha-secret-key " value = " ...[secret key] " />
< add key = " recaptcha-public-key " value = " ...[public-key] " />Facultatif si vous souhaitez modifier la langue par défaut du captcha (obtenez le code de langue sur le site recaptcha: https://developers.google.com/recaptcha/docs/language) ou vous pouvez utiliser la valeur "auto" et il obtiendra la langue de System.thread.currentCulture
< add key = " recaptcha-language-key " value = " [language-code] " />Ou via le code C #: il n'est nécessaire de l'appeler une seule fois, un bon endroit pour le mettre sur la fonction 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 ) ;Facultatif si vous souhaitez modifier le thème par défaut du mode clair au mode sombre.
< add key = " recaptcha-language-theme " value = " dark " />Ou via le code C #:
//Optional select a default dark theme
ReCaptchaLanguage defaultLanguage = ReCaptchaLanguage . Auto ;
ReCaptcha . Configure ( publicKey , secretKey , defaultLanguage , ReCaptchaTheme . dark ) ; À l'intérieur de votre formulaire
< 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 >Facultatif si vous souhaitez remplacer votre langue par défaut configurée:
< form action =" myAction " >
< input type =" text " name =" myinput1 " />
@ReCaptcha.GetCaptcha(ReCaptchaLanguage.PortugueseBrazil) <!-- Will show your ReCaptcha as Portuguese,
overriding any previous configuration -->
</ form >Facultatif si vous souhaitez remplacer votre thème configuré (clair / sombre):
< 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 >À l'intérieur de votre formulaire
< 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 >Facultatif si vous souhaitez remplacer votre langue par défaut configurée:
< 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 >À l'intérieur de votre fonction de contrôleur ou dans un filtre
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" ) ;
} Vous pouvez utiliser un proxy pour envoyer une réponse de l'utilisateur sur le serveur RecaptCha
.. .
const string proxyIp = "xxx.xxx.xxx.xxx" ;
const int proxyPort = 1234 ;
WebProxy webProxy = new WebProxy ( proxyIp , proxyPort ) ;
bool validCaptcha = ReCaptcha . ValidateCaptcha ( userResponse , webProxy ) ;
.. .Peut lancer l'exception suivante, si la clé secrète n'est pas valide, ou si vous passez une réponse d'utilisateur non valide comme paramètre validatecaptcha:
throw new ReCaptchaException ( ) ;Il peut également être appelé asynchrone:
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" ) ;
}
}