ReCaptcha ASP.NET MVC latest version wrapper
https://www.google.com/recaptcha
Install-Package reCaptcha.AspNet.Mvc 1) Get Google reCAPTCHA at https://www.google.com/recaptcha
<addkey="ReCaptcha:SiteKey"value="your-site-key" /> <addkey="ReCaptcha:SecretKey"value="your-secret-key" />Add
using reCaptcha;to the top of your C# file see MSDN
publicclassAccountsController:Controller{[HttpGet]publicActionResultRegister(){ViewBag.Recaptcha=ReCaptcha.GetHtml(ConfigurationManager.AppSettings["ReCaptcha:SiteKey"]);ViewBag.publicKey=ConfigurationManager.AppSettings["ReCaptcha:SiteKey"];returnView();}[HttpPost][ValidateAntiForgeryToken]publicActionResultRegister(RegisterMerchantViewModelrequest){try{if(ModelState.IsValid&&ReCaptcha.Validate(ConfigurationManager.AppSettings["ReCaptcha:SecretKey"])){// Do what you needreturnView("RegisterConfirmation");}ViewBag.RecaptchaLastErrors=ReCaptcha.GetLastErrors(this.HttpContext);ViewBag.publicKey=ConfigurationManager.AppSettings["ReCaptcha:SiteKey"];returnView(request);}catch(Exception){returnnewHttpStatusCodeResult(HttpStatusCode.InternalServerError);}}}Before you can use the Helper you should declare using it. By add
@using reCaptchato the top of your Razor file. See Link#14
if you need multiple instance on one page please skip to next header
Add the following code to your Views/Merchants/Register.cshtml:
@ReCaptcha.GetHtml(@ViewBag.publicKey) @if (ViewBag.RecaptchaLastErrors!=null){<div>Oops! Invalid reCAPTCHA=(</div>}introduced in 1.2.3 requested in #11
@ReCaptcha.GetExplictHtml("example1",@ViewBag.publicKey) @ReCaptcha.GetExplictHtml("example2",@ViewBag.publicKey) @ReCaptcha.GetExplictScript() @if (ViewBag.RecaptchaLastErrors!=null){<div>Oops! Invalid reCAPTCHA=(</div>}Let's talk more about the most basic way to get started:
@ReCaptcha.GetHtml("site-key")The synopsis for the @ReCaptcha.GetHtml function is:
@ReCaptcha.GetHtml(publicKey, [theme], [type], [callback], [lang])ReCaptcha Parameter reCaptcha doc
| key | value | default | description |
|---|---|---|---|
publicKey | Your sitekey. | ||
theme | dark/light | light | Optional. The color theme of the widget. |
type | audio/image | image | Optional. The type of CAPTCHA to serve. |
callback | Optional. Your callback function that's executed when the user submits a successful CAPTCHA response. The user's response, g-recaptcha-response, will be the input for your callback function. | ||
lang | See language codes | Optional. Forces the widget to render in a specific language. Auto-detects the user's language if unspecified. |
For enabling multi captcha in one page. please check the example for Explicit rendering for multiple widgets
simple use
@ReCaptcha.GetExplictHtml("id","site-key")The synopsis for the @ReCaptcha.GetExplictHtml function is:
@ReCaptcha.GetExplictHtml(id, publicKey, [widgetRenderCallsArr], [theme], [type], [callback])ReCaptcha Parameter reCaptcha doc
| key | value | default | description |
|---|---|---|---|
id | the recaptcha widget id. required uniquely identifies the recaptcha widget | ||
publicKey | Your sitekey. | ||
widgetRenderCallsArr | __recaptcha_widgetRenderCallsArr | js variable name for recaptcha render calls. | |
theme | dark/light | light | Optional. The color theme of the widget. |
type | audio/image | image | Optional. The type of CAPTCHA to serve. |
callback | Optional. Your callback function that's executed when the user submits a successful CAPTCHA response. The user's response, g-recaptcha-response, will be the input for your callback function. |
@ReCaptcha.GetExplictScript()The synopsis for the @ReCaptcha.GetExplictScript function is:
@ReCaptcha.GetExplictScript([lang], [load], [widgetRenderCallsArr])| key | value | default | description |
|---|---|---|---|
lang | See language codes | Optional. Forces the widget to render in a specific language. Auto-detects the user's language if unspecified. | |
load | __recaptcha_onloadCallback | js variable name for recaptcha on load explicit call. | |
widgetRenderCallsArr | __recaptcha_widgetRenderCallsArr | js variable name for recaptcha render calls. |
see recaptcha doc
returns true for valid response from user, false otherwise.
privateKey 'Secret key' is Required. The shared key between your site and ReCAPTCHA.
see recaptcha doc
returns a IEnumerable<reCaptcha.ErrorCodes>. if returns null the no errors occurred.
context is your HttpContenxt e.g. this.HttpContext