Click here to Skip to main content
15,892,005 members
Articles / Web Development / ASP.NET

How to Add Captcha to your ASP.NET WebForms Project in 5 Minutes

Rate me:
Please Sign up or sign in to vote.
4.78/5 (5 votes)
15 May 2014CPOL1 min read 48.2K   10   6
Here is how to add captcha to your ASP.NET Webforms project in 5 minutes

Spamming is one of the issues we all are trying to deal with. One of the major ways of spamming is to submit forms automatically on your website. Captcha is one of the ways that deals with automated form submission by displaying random image to a user and asking the user to fill in the letters/numbers as seen in the image. Today, we will show how you can add Captcha to any of your forms in ASP.NET web forms within 5 minutes.

To achieve this, we have to follow the following steps:

  • If you want captcha to add to your existing project, you don’t need to do the next 2 steps mentioned below.
  • Create a new Visual Studio project.
  • Add some input controls on the page for submission.
  • Add a Nuget Package named “BotDetect” to our project. To do this, you can follow the steps given below:
    • Right click on “References” on your project in the Solution Explorer
    • Click on “Manage Nuget Packages”
    • Search for BotDetect and install the one that is created by Captcha, Inc. Below is a screenshot for reference.

      nuget

  • In your ASPX page, where you need to display the captcha, write the below 2 lines of code:
    ASP.NET
    <asp:textbox ID="txtCaptcha" runat="server"></asp:textbox>
    <botdetect:captcha ID="captchaBox" runat="server"></botdetect:captcha>
    
  • In your ASPX.cs page, validate your captcha code with the code below before processing the submission.
    C#
    bool isHuman = captchaBox.Validate(txtCaptcha.Text);
    txtCaptcha.Text = null;
    if (!isHuman)
    {
        //The Captcha entered by user is Invalid.
    }
    else
    {
        //The Captcha entered by user is Valid.
    }
    
  • You’re done.

You can see the output of the above captcha below:

captcha-live

Hope you like this! Keep learning and sharing! Cheers!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Rebin Infotech
India India
A passionate developer with over 10 years of experience and building my software company code by code. Experience withMS Technologies like .Net | MVC | Xamarin | Sharepoint | MS Project Server and PhP along with open source CMS Systems like Wordpress/DotNetNuke etc.

Love to debug problems and solve them. I love writing articles on my website in my spare time. Please visit my Website for more details and subscribe to get technology related tips/tricks. #SOreadytohelp

Comments and Discussions

 
QuestionSmooth! Pin
rushmore323829-Sep-20 0:23
rushmore323829-Sep-20 0:23 
QuestionImages not loading Pin
Anuj Chauhan from India20-Jul-19 5:36
Anuj Chauhan from India20-Jul-19 5:36 
AnswerRe: Images not loading Pin
Anuj Chauhan from India20-Jul-19 5:52
Anuj Chauhan from India20-Jul-19 5:52 
QuestionA mistake in the code Pin
Member 1411773814-Jan-19 5:40
Member 1411773814-Jan-19 5:40 
QuestionHow to register this control Pin
meeram3915-May-14 23:15
professionalmeeram3915-May-14 23:15 
Do we need to register this control in our aspx page? I believe that it will end up in error if not registered.
AnswerRe: How to register this control Pin
Nitesh Kejriwal15-May-14 23:17
professionalNitesh Kejriwal15-May-14 23:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.