Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
15 May 2014 1  
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:
  • In your ASPX page, where you need to display the captcha, write the below 2 lines of code:
    <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.
    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:

<img alt="captcha-live" class="aligncenter size-full wp-image-1079" src="773785/captcha-live.png" style="width: 404px; height: 423px;" />

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here