Easily Implementing CAPTCHAs in C#

Introduction

A CAPTCHA (acronym for "Completely Automated Public Turing test to tell Computers and Humans Apart") is a type of challenge-response test used in computing to determine whether or not the user is human.

We are all aware of the requirement of a “CAPTCHA Library” or plugin to generate a CAPTCHA image or text based image. But it may require more time to develop your product for a developer. Since we are always hungry to grab everything in time and in hand it in in the softest way. We all belong to the Ninja Generation (Fastest).

We can simply download or generate a few images having some text or numbers. A good number of image editors currently are freely available in the market now-a-days. So I can expect it to be easy. After obtaining these images put all of them in the Images folder of your Visual Developer Solution file.

Now for the actual implementation. Select the “ASP” file or WebForm where you want to invoke your CAPTCHA. I created there an image button followed by a TextBox (where the user will enter data written in a CAPTCHA image). Then go to the aspx.cs file and provide the following code segment within Page Load.

  1. static int a = 0;  
  2. //declaring a as static variable  
  3. protected void Page_Load(object sender, EventArgs e)  
  4. {  
  5.     if (!IsPostBack) //Check whether this is pos back occurred or not  
  6.     {  
  7.         DateTime now = DateTime.Now;  
  8.         int s = now.Second;  
  9.         a = s % 10;  
  10.         if (a == 0)  
  11.         {  
  12.             ImageButton1.ImageUrl = "~/imge/0.jpg"//captcha image 1  
  13.         }  
  14.         if(a == 1)  
  15.         {  
  16.             ImageButton1.ImageUrl = "~/imge/1.jpg"//captcha image 2  
  17.         }  
  18.         ……………………………………………………………..  
  19.         …………………………………………………….  
  20.     }  
  21. }  
  22. // Suppose Captcha image 1 (0.jpg) contains “abc”, Captcha 2 Image (1.jpg) contains “def”  
  23. //then we have to evaluate the contents of textbox1 in button click event of .cs file.  
  24. // Response.Redirect(Request.RawUrl.ToString()) is used to reload or refresh the page itself.  
  25. protected void Button1_Click(object sender, EventArgs e)  
  26. {  
  27.     switch (a)  
  28.     {  
  29.         case 0:  
  30.             if (TextBox1.Text == "abc")  
  31.             {  
  32.                 Response.Write("You are a man…");  
  33.             }   
  34.             else   
  35.             {  
  36.                 Response.Write("Robot Found.. Try Again");  
  37.                 Response.Redirect(Request.RawUrl.ToString());  
  38.             }  
  39.             break;  
  40.         case 1:  
  41.             if (TextBox1.Text == "def")  
  42.             {  
  43.                 Response.Write("You are a man..");  
  44.             }   
  45.             else   
  46.             {  
  47.                 Response.Write("Robot found .. try again");  
  48.                 Response.Redirect(Request.RawUrl.ToString());  
  49.             }  
  50.             break;  
  51.             ………………………………………………….  
  52.             ……………………………………………………………………………………………………  
  53.             //We can extend it more as per our requirement.  
  54.     }  
  55. }  


Similar Articles