21

can any one tell me how to use captcha in asp.net mvc? is there any need to download any control for it?

bkaid
  • 51,465
  • 22
  • 112
  • 128
mary
  • 431
  • 3
  • 5
  • 11
  • 1
    possible duplicate of [What is a CAPTCHA that is compatible with ASP.NET MVC ?](http://stackoverflow.com/questions/258897/what-is-a-captcha-that-is-compatible-with-asp-net-mvc) – Gabe Mar 24 '11 at 02:19
  • Check this Link http://devlicio.us/blogs/derik_whittaker/archive/2008/12/02/using-recaptcha-with-asp-net-mvc.aspx Will Definitely Help. – coolguy97 Mar 25 '10 at 09:21
  • Another blog post: [here at thenullreference.com](http://thenullreference.com/blog/robots-gotcha-down-get-recaptcha-in-asp-net-mvc/) – Greg Roberts Feb 18 '10 at 06:58
  • 1
    This [blog post](http://www.coderjournal.com/2008/03/aspnet-mvc-captcha/) shows you how to do it. – Perpetualcoder Feb 18 '10 at 06:19
  • i tried form this blog but still image is not displying plz tell me another solution i want to use captch only not recaptcha. – mary Feb 18 '10 at 07:21
  • put some code sample in the question. Maybe we can help – Perpetualcoder Feb 18 '10 at 16:00

2 Answers2

36

Hoping it's not too late to put my two cents in...

Introducing MvcReCaptcha

I faced this exact same issue when trying to implement CAPTCHA validation on my first ASP.NET MVC site. After discovering many libraries, I found what seemed (and still seems) to be the most straightforward and efficient library: MvcReCaptcha. Since then, I have used this library for all of my ASP.NET MVC sites.

After you implement MvcReCaptcha, it securely generates a CAPTCHA on your view and provides a boolean value of whether the validation was successful to the action.


Instructions for Use

Here's how to implement it after downloading and referencing the MvcReCaptcha DLL from your project (instructions copied from MvcReCaptcha home page):

Using ReCaptcha with ASP.NET MVC:

It is now extremely easy to setup ReCaptcha on your Asp.Net MVC Website.

Signup for reCaptcha, http://recaptcha.net/whyrecaptcha.html

How to Use:

Step 1: Add your Public and Private key to your web.config file in appsettings section

<appSettings>
  <add key="ReCaptchaPrivateKey" value=" -- PRIVATE_KEY -- " />
  <add key="ReCaptchaPublicKey" value=" -- PUBLIC KEY -- " />
</appSettings>    

Step 2: Add new namespace to your web.config

<namespaces>
  <add namespace="MvcReCaptcha.Helpers"/>
</namespaces>

Step 3: Implement the logic in your view to actually render the Captcha control

<%= Html.GenerateCaptcha() %>

Step 4: Implement the Controller Action that will handle the form submission and Captcha validation

[CaptchaValidator]
[AcceptVerbs( HttpVerbs.Post )]
public ActionResult CreateComment( Int32 id, bool captchaValid )
{
  if (!captchaValid)
  {
      ModelState.AddModelError("_FORM", "You did not type the verification word correctly. Please try again.");
  }
  else
  {
      // If we got this far, something failed, redisplay form
      return View();
  }
}

Good luck!

Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173
  • 1
    very through. tried it and worked perfectly fine. I am now stuck with net 3.5 so Microsoft.web.helpers is not compatible with net 3.5 and this is the solution. – tugberk Feb 24 '11 at 16:00
  • @tugberk_ugurlu_: Great to hear. Best of luck! – Maxim Zaslavsky Feb 25 '11 at 00:07
  • 6
    For .NET 4.0 MVC 3 Razor, you will need to import the MvcReCaptcha project in your solution, change the target to .NET 4, remove the System.Web.Mvc reference (it references the old version by default), add System.Web.Mvc v3.0.0.0, rebuild and reference this project rather than the DLL the project comes bundled. In your Razor file add @Html.Raw(Html.GenerateCaptcha()) or you could modify GenerateCaptcha to return MvcHtmlString. Everything else works the same. – pbz Dec 07 '11 at 21:17
  • 1
    If you're using SSL, you also need to go to http://code.google.com/apis/recaptcha/docs/aspnet.html and download the latest binary and replace the recaptcha.dll assembly in the MvcReCaptcha project. – pbz Dec 07 '11 at 21:52
  • You can also use NuGet to get the .NET package, then you only have to add the application settings (the keys), everything else just works. – Pawel Krakowiak Jan 13 '12 at 15:18
  • 1
    @PawelKrakowiak what is the Nuget package name? – DROP TABLE users Jan 23 '14 at 21:42
  • @pbz the link you provided no longer works: do you have an updated link? – Milliron X Dec 22 '16 at 05:19
2

If you don't fancy writing your own Captcha (who does!) you can use a Captcha library, such as this:

http://www.coderjournal.com/2008/03/aspnet-mvc-captcha/

With a Captcha library, you add the dll to your project and use the Captcha API to display and validate the Captcha image and input.

Display a Captcha:

<label for="captcha">Enter <%= Html.CaptchaImage(50, 180) %> Below</label><br />
<%= Html.TextBox("captcha") %>

And then make sure you add the Captcha attribute to your method:

[CaptchaValidation("captcha")]

Recaptcha is just one option when it comes to Captcha's (in fact, it's the option selected by Stack Overflow!)

Fenton
  • 241,084
  • 71
  • 387
  • 401
  • NuGet **Google reCAPTCHA V2** for MVC 4 and 5 - [NuGet Package](https://www.nuget.org/packages/reCAPTCH.MVC/) - [Demo And Document](http://recaptchamvc.apphb.com/) – Sender Jun 16 '15 at 13:49