Skip to content

wildcard/ReCaptcha-ASP.NET-MVC

Repository files navigation

ReCaptcha-ASP.NET-MVC Twitter

Join the chat at https://gitter.im/wildcard/ReCaptcha-ASP.NET-MVCNuGet StatusNuGet Version

ReCaptcha ASP.NET MVC latest version wrapper

https://www.google.com/recaptcha

Install via Nuget

Install-Package reCaptcha.AspNet.Mvc 

Simple usage

1) Get Google reCAPTCHA at https://www.google.com/recaptcha

2) Add Site key and Secret key to your Web.config

<addkey="ReCaptcha:SiteKey"value="your-site-key" /> <addkey="ReCaptcha:SecretKey"value="your-secret-key" />

3) Add server-side integration to your back-end

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);}}}

4) Add client-side integration to your front-end

Before you can use the Helper you should declare using it. By add @using reCaptcha to 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>}

Add client-side integration to your front-end for explicit use or multiple instance

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>}

Done!

Usage

@ReCaptcha.GetHtml(...)

Let's talk more about the most basic way to get started:

@ReCaptcha.GetHtml("site-key")

Arguments

The synopsis for the @ReCaptcha.GetHtml function is:

@ReCaptcha.GetHtml(publicKey, [theme], [type], [callback], [lang])
ReCaptcha Parameter reCaptcha doc
keyvaluedefaultdescription
publicKeyYour sitekey.
themedark/lightlightOptional. The color theme of the widget.
typeaudio/imageimageOptional. The type of CAPTCHA to serve.
callbackOptional. 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.
langSee language codesOptional. Forces the widget to render in a specific language. Auto-detects the user's language if unspecified.

@ReCaptcha.GetExplictHtml(...)

For enabling multi captcha in one page. please check the example for Explicit rendering for multiple widgets

simple use

@ReCaptcha.GetExplictHtml("id","site-key")

Arguments

The synopsis for the @ReCaptcha.GetExplictHtml function is:

@ReCaptcha.GetExplictHtml(id, publicKey, [widgetRenderCallsArr], [theme], [type], [callback])
ReCaptcha Parameter reCaptcha doc
keyvaluedefaultdescription
idthe recaptcha widget id. required uniquely identifies the recaptcha widget
publicKeyYour sitekey.
widgetRenderCallsArr__recaptcha_widgetRenderCallsArrjs variable name for recaptcha render calls.
themedark/lightlightOptional. The color theme of the widget.
typeaudio/imageimageOptional. The type of CAPTCHA to serve.
callbackOptional. 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([lang], [load], [widgetRenderCallsArr])

@ReCaptcha.GetExplictScript()

Arguments

The synopsis for the @ReCaptcha.GetExplictScript function is:

@ReCaptcha.GetExplictScript([lang], [load], [widgetRenderCallsArr])
ReCaptcha Parameter
keyvaluedefaultdescription
langSee language codesOptional. Forces the widget to render in a specific language. Auto-detects the user's language if unspecified.
load__recaptcha_onloadCallbackjs variable name for recaptcha on load explicit call.
widgetRenderCallsArr__recaptcha_widgetRenderCallsArrjs variable name for recaptcha render calls.

@ReCaptcha.Validate(privateKey)

see recaptcha doc

returns true for valid response from user, false otherwise.

ReCaptcha Parameter

privateKey 'Secret key' is Required. The shared key between your site and ReCAPTCHA.

@ReCaptcha.GetLastErrors(HttpContextBase context)

see recaptcha doc

returns a IEnumerable<reCaptcha.ErrorCodes>. if returns null the no errors occurred.

ReCaptcha Parameter

context is your HttpContenxt e.g. this.HttpContext

About

ReCaptcha ASP.NET MVC version 2.0 wrapper

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5

Languages