Creating CAPTCHA in c#.net

This article introduces how to create CAPTCHA in c#.net. Fully CAPTCHA known as “Completely Automated Public Turing Test To Tell Computers and Humans Apart”. CAPTCHA is generate test/challenges that only human can solve but that not solve by programs.

Nowadays, the hackers are creating some programs known as robot or bots. That basically used for different kind of attacks like data flooding, DoS or spam.

To avoid such attacks, we will make a program that will generate two numbers, for summation. Both the number random and changed every time. Challenge given to user is to make sum of them that only human can do.

Introduction

We are creating CAPTCHA application for program use in ASP.net with (c#).

Requirement

1. Two Label: for holding to random number.

2. Two textbox: one for user and other will remain hidden, which hold the sum of random numbers calculated by machine.

3. Validator: comparevalidator, for comparing answer given by user and answer calculated by machine.

4. Submit button: To SUBMIT data.

I have place all the control given in image 1.


[Image 1]

Go to the design view, and set the custom validator property and given in image 2.1


[Image 2.1]

At the code behind page, we are going to write the code given in Image 2.


[Image 2]

Code explanation

In 1st I have declare 2 variable no1 and no2 in store random number. In

Next line I created r object of random class.

In 3 and 4 line we are generating the random number from 0 to 9.

r.next(0,9);

in 5 and 6 line we are displaying the number in the label 1 and 2.

At 7th line we are storing sum of no1 and no2.

All this code within if conditions of post back. If you don’t do this you code might not function properly.


[Image 3]

When you run this program it will look like this image 3. If you give correct answer than custom validator will not win to show any error, but if you give the wrong answer will display an error look like image 4.


[Image 4]

Applications of CAPTCHAs

1. Preventing comment spam in blogs.

2. Providing website registration.

3. Online polls

4. Preventing dictionary attack

5. Search engine bots

Share Post