How to implement Google reCaptcha in MVC3 application?

Can anyone explain how to use reCaptcha features like stackoverflow in my MVC3 application.

And how can you customize this?

+8
asp.net-mvc recaptcha asp.net-mvc-3
source share
2 answers

I use Google ReCaptcha and it works very well and is very easy to implement.

Please note: if you use Https, make sure that you currently have the current version of the dll (1.0.5.0)

You need to create an account on the Google Recaptcha website and get a set of public and private keys. Add the keys to the main web.config file of the web project:

<appSettings> <add key="webpages:Version" value="1.0.0.0"/> <add key="ClientValidationEnabled" value="true"/> <add key="UnobtrusiveJavaScriptEnabled" value="true"/> <add key="ReCaptchaPrivateKey" value="put your private key value here" /> <add key="ReCaptchaPublicKey" value="put your public key value here" /> </appSettings> 

Now use NuGet and install the reCAPTCHA plugin for .NET.

Then go to the web.config file inside your VIEWS folder. Add this line:

 <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Routing" /> <add namespace="Recaptcha"/> </namespaces> 

Then, in your opinion, that you want to show captcha, add a using statement at the top of your file

 @using Recaptcha; 

then add this to your view:

 <div class="editor-label"> Are you a human? </div> <div class="editor-field"> @Html.Raw(Html.GenerateCaptcha("captcha", "clean")) @Html.ValidationMessage("captcha") </div> 

In your controller action, you will need to change the signature in order to accept the tracking results:

 [HttpPost] [RecaptchaControlMvc.CaptchaValidator] public ActionResult ForgotPassword(CheckUsernameViewModel model, bool captchaValid, string captchaErrorMessage) { if (!Membership.EnablePasswordReset) throw new Exception("Password reset is not allowed\r\n"); if(ModelState.IsValid) { if(captchaValid) { return RedirectToAction("AnswerSecurityQuestion", new { username = model.Username }); } ModelState.AddModelError("", captchaErrorMessage); } return View(model); } 

Following these steps, I allowed to implement captcha on several pages, and it works smoothly. Note that the parameter names in the controller action MUST BE THE NAME CORRECTLY:

 bool captchaValid, string captchaErrorMessage 

If you change these parameter names, you will receive a runtime error when your form returns to the controller action.

+30
source share

I would recommend using Honeypot Captcha. The experience for your users is much better. There is one preliminary ASP.NET MVC here http://nuget.org/packages/SimpleHoneypot.MVC

 PM> Install-Package SimpleHoneypot.MVC4 

There is WiKi on how to get it: https://github.com/webadvanced/Honeypot-MVC/wiki Just start with the Getting Started section.

You can learn more about the general idea of โ€‹โ€‹Honeypot Captcha here: http://haacked.com/archive/2007/09/11/honeypot-captcha.aspx

+3
source share

All Articles