CAPTCHAs
A CAPTCHA is a validation layer to recognize the type of user before sending data to the server. Advantages of CAPTCHAs
There are many tools to send automated messages. If you have not implemented a CAPTCHA in your feedback or user registration page then you will get many spam messages or users.So the main advantages of CAPTCHA is to avoid spam messages or users.How to use CAPTCHA in ASP.NET MVC
There are many open-source libraries available to do this. Even though you can write your own code to implement a CAPTCHA in your application, that totally depends on you.See the following images.

Char CAPTCHA Example

After CAPTCHA validation

Math CAPTCHA Example
Step 1
Create the Empty ASP.NET MVC application.
Step 2
Add the CaptchaMvc library to the Reference Layer like this.

Step 3
In the Model layer add a Feedback class and create the property like this.
- namespace MathCaptcha.Models
- {
- public class Feedback
- {
- public int Id { get; set; }
- public string Title { get; set; }
- public string Comment { get; set; }
-
- }
- }
Step 4
Create a HomeController and write the action method like this.
- using System.Web.Mvc;
- using CaptchaMvc.HtmlHelpers;
-
-
- namespace MathCaptcha.Controllers
- {
- public class HomeController : Controller
- {
-
- public ActionResult Index()
- {
- return View();
- }
-
- [HttpPost]
- public ActionResult Index(string empty)
- {
-
- if (this.IsCaptchaValid("Captcha is not valid"))
- {
-
- return RedirectToAction("ThankYouPage");
- }
-
- ViewBag.ErrMessage = "Error: captcha is not valid.";
- return View();
- }
-
- public ActionResult ThankYouPage()
- {
- return View();
- }
-
- }
- }
Note: Do not forget to use the CAPTCHA MVC.HtmlHelpers namespace. In the post action method I wrote code for CAPTCHA validation.
Step 5
Now create the Index View like this.
- @using CaptchaMvc.HtmlHelpers
- @using MathCaptcha;
- @using CaptchaMvc;
-
- @model MathCaptcha.Models.Feedback
-
- @{
- ViewBag.Title = "Index";
- }
-
- @using (Html.BeginForm()) {
- @Html.ValidationSummary(true)
-
- <fieldset>
- <legend>Feedback</legend>
-
- <div class="editor-label">
- @Html.LabelFor(model => model.Title)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Title)
- @Html.ValidationMessageFor(model => model.Title)
- </div>
-
- <div class="editor-label">
- @Html.LabelFor(model => model.Comment)
- </div>
- <div class="editor-field">
- @Html.TextAreaFor(model => model.Comment,5,40,null)
- @Html.ValidationMessageFor(model => model.Comment)
- </div>
-
- @Html.MathCaptcha()
-
- @*@Html.Captcha(3)*@
- <br />
- <p class="Error"> @ViewBag.ErrMessage </p>
- <p>
- <input type="submit" value="Send" />
- </p>
- </fieldset>
- }
Note: Here if you use the @Html.MathCaptcha() helper class then it will generate a mathmatical CAPTCHA. If you use the @Html. Captcha(3) helper class then it will generate a Char CAPTCHA. 3 is the length of the CAPTCHA.
Step 6: Create the ThankYouPage index like this.
- @model MathCaptcha.Models.Feedback
-
- @{
- ViewBag.Title = "ThankYouPage";
- }
-
- <h2>ThankYouPage</h2>
-
- <b> Thank you sending your valuable feedback to us.</b>
Points to remember
- Just include the CaptchaMvc libray.
- Use the CaptchaMvc.HtmlHelpers namespace.
- Use the MathCaptcha namespace for using mathCaptcha.
- Use the CaptchaMvc namespace for using CharCaptcha.
- For a math CAPTCHA use the @Html.MathCaptcha() helper class.
- For a Char CAPTCHA use the @Html.Captcha(3) helper class.
Summary
Here we saw how to use a CAPTCHA in an ASP.NET MVC application. It is very simple and straight forward to implement in MVC applications.
Oktay SERINKAYAPosted May 6, 2024, 2:56 PM
After placing character captcha in my view, there is an input box with label saying "Input symbols". How can I remove this label or translate it into my language?
suresh goliPosted Oct 26, 2022, 9:43 AM
How to show the refresh button to refresh the captcha
chanchal karmaPosted Aug 8, 2022, 11:22 AM
ITs not working....
Anil GangwarPosted Mar 29, 2022, 3:38 AM
It's not working on case sensitive format
Dotnet TeamPosted Nov 24, 2021, 6:19 AM
Hi,When click refresh button page is loading ...how to prevent it? i mean when i click refresh button it should not load page again...
Syed AkhterPosted Sep 1, 2021, 5:49 AM
For alpha numeric and case sensitive captcha [ https://stackoverflow.com/q/68993372/11195857 ]
Sajina N SPosted Aug 16, 2021, 6:25 AM
Hi Chandradev, not showing captcha image after implementing like above
NITESH KUMARPosted Aug 10, 2020, 10:05 AM
Hello Chandradev, this.IsCaptchaValid("Invalid Captcha") method always returning false. I have 2 methods, first method is to show the login page and 2nd method is for Post request.I am using Ajax to post data, passing form data as JSON.stringify(jsonObject) I am not using any Model class. I am able to see the Captcha on the view. But I don't know why this.Captcha always returing false.
Erhan OymacıPosted May 19, 2020, 5:39 PM
I send the security code incorrectly using the "Ajax.BeginForm" and then when I send it correctly again, it doesn't accept the security code. when I refresh the security code, the button disappears after clicking on it and" Failed to load resource: the server responded with a status of 500 (Internal Server Error) " jQuery gives this error. How I can solve.
Virender RawatPosted May 18, 2020, 2:41 PM
IN PRODUCTION @Html.Captcha(5) giving object refrence error in localhost it is working fine
Safvan AKPosted May 13, 2020, 4:52 AM
HI, i have tried this but not showing the image
Saroj MahantaPosted Jan 30, 2020, 2:47 PM
Hello Chandradev, Nice to see such a detail information about Captcha. Thanks for all the efforts and sharing your knowledge with everyone. Well, i am a complete novice in this area and never used Captcha before in my code. Would you like to share your whatsapp number so that i can connect to you for further discussion on this topic, please ? my email id is [email protected]
Chandradev PrasadPosted Nov 30, 2019, 11:14 AM
Could you please share me the code ?
Aashish JainPosted Nov 28, 2019, 4:40 AM
Hi Chandradev, Could you pls help me i was implement same with ajax call then it's not working plase help me for same
Adhan MaldonadoPosted May 29, 2018, 10:02 AM
Where is the implementation to methods IsCaptchaValid ?
Arun SikarwarPosted Apr 7, 2018, 4:41 AM
If i call this action method thorough ajax call it's not working
Indu YadavPosted Jul 8, 2017, 3:38 AM
Refresh link showing here when i click on it to refresh a new captcha page is redirecting into #CaptchaImage...................how to resolve this issue that when i refresh captcha page not redirecting in mvc 4
Tk MahantaPosted Apr 4, 2017, 2:39 AM
Very helpful to all
Manav PandyaPosted Oct 15, 2016, 6:06 AM
Nice share ...
Dhrutika RathodPosted Sep 27, 2016, 12:27 AM
Try another button is hide once click on that
Kaushal BhavsarPosted Jun 17, 2016, 3:27 PM
It is not case sensitive. How to implement case sensitive Chapcha?
Harshitha.R. ChandraPosted May 27, 2016, 5:56 AM
How can i include audio to this captcha? Please help me...
Reza TanzifiPosted Aug 26, 2015, 6:29 AM
This is Not a Good solution!!!
Chandradev PrasadPosted Aug 24, 2015, 7:38 AM
@Saineshwar Bageri , if you will refresh browser or if will enter invalid code then the chapcha code will change.
Saineshwar BageriPosted Aug 20, 2015, 6:55 AM
Sir it does not have refresh button
Joginder BangerPosted Jun 4, 2015, 4:36 PM
Good job sir...
Upendra Pratap ShahiPosted Jun 1, 2015, 8:53 AM
nice..
Saineshwar BageriPosted May 11, 2015, 5:44 AM
finding error in captcha while using i am not finding @Html.MathCaptcha() and also provide file to download
ramakrishna muniPosted May 8, 2015, 6:35 AM
Thanq sirhow to reduce captcha image size
nagendra prasadPosted Apr 28, 2015, 3:03 AM
how to handle client side using jquery validation with ajax call
lakshmanamurthy sanaPosted Feb 9, 2015, 5:29 AM
Nice
Chandradev PrasadPosted Nov 11, 2014, 4:03 AM
Thank you.
Guest UserPosted Nov 10, 2014, 9:46 PM
In past I'd used Google reCaptcha.
Guest UserPosted Nov 10, 2014, 9:46 PM
good to know this CaptchaMvc library
Chandradev PrasadPosted Nov 10, 2014, 10:30 AM
Nice to know that you liked it. Thank you.
Saineshwar BageriPosted Nov 10, 2014, 6:04 AM
nice one sir