15/05/2014 by Nitesh

How To Add Captcha To Your ASP.Net WebForms Project in 5 minutes

Friends,

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 int he 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 following steps-
    • 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.
            
            
    
  • In your ASPX.cs page, validate your captcha code with the code below before processing the submission.
       
                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.
  • Please note that adding the DLL reference adds a couple of settings in web.config file. If you are adding it to your existing project, make sure you add the below settings in your web.config file.
    • Under <system.web> -> <pages> -> <controls>, add the below statement
      
      
    • Under <system.webServer> -> <handlers> section, add the below statement
              
      	
      

You can see the output of above captcha below –

captcha-live
Hope you like this! Keep learning and sharing! Cheers!

#ASP.Net#Captcha#Utilities