Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use Captcha in asp.net mvc [closed]

Tags:

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

like image 611
mary Avatar asked Feb 18 '10 06:02

mary


2 Answers

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!

like image 150
Maxim Zaslavsky Avatar answered Nov 08 '22 16:11

Maxim Zaslavsky


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!)

like image 41
Fenton Avatar answered Nov 08 '22 17:11

Fenton