Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET
Article

Simple Captcha with ASP.NET

Rate me:
Please Sign up or sign in to vote.
2.68/5 (13 votes)
5 Aug 2005 72.5K   46   7
Simple text to image generator to block spammers inserting data to your database, with ASP.NET.

Introduction

I was trying to find a solution to block spammers adding data to the database, using C#. This is the quick solution I found.

  1. First create an image tag in the webform that has the submit form.
    HTML
    <IMG height="30" alt="" src="Turing.aspx" width="80">

    As you see, the source for the picture will be a file called “Turing.aspx”.

  2. Later, create the webform “Turing.aspx” with the code below:
    C#
    public class Turing1 : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
            Bitmap objBMP =new System.Drawing.Bitmap(60,20);
            Graphics objGraphics = System.Drawing.Graphics.FromImage(objBMP);
            objGraphics.Clear(Color.Green);
    
            objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
    
            //' Configure font to use for text
            Font objFont = new  Font("Arial", 8, FontStyle.Bold);
            string randomStr="";
            int[] myIntArray = new int[5] ;
            int x;
    
            //That is to create the random # and add it to our string
            Random autoRand = new Random();
    
            for (x=0;x<5;x++)
            {
                myIntArray[x] =  System.Convert.ToInt32 (autoRand.Next(0,9));
                randomStr+= (myIntArray[x].ToString ());
            }
    
            //This is to add the string to session cookie, to be compared later
            Session.Add("randomStr",randomStr);
    
            //' Write out the text
            objGraphics.DrawString(randomStr, objFont, Brushes.White,  3, 3);
    
            //' Set the content type and return the image
            Response.ContentType = "image/GIF";
            objBMP.Save(Response.OutputStream, ImageFormat.Gif);
    
            objFont.Dispose();
            objGraphics.Dispose();
            objBMP.Dispose();
       }
    }
  3. And this is the code for the Submit button on the main form:
    C#
    private void btnSubmit_ServerClick(object sender, System.EventArgs e)
    {
        if (Page.IsValid && (txtTuring.Value.ToString () == 
                                Session["randomStr"].ToString ())) 
        {
             // The Code to insert data
        }
        else
        {
            Label1.Text ="Please enter info correctly";
        }
    }

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


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 103557736-Jun-16 11:28
Member 103557736-Jun-16 11:28 
QuestionIt gives all you need! Pin
Homero Rivera3-Sep-14 18:12
Homero Rivera3-Sep-14 18:12 
GeneralThis won't block spammers! Pin
JJF0075-Aug-05 4:55
JJF0075-Aug-05 4:55 
GeneralRe: This won't block spammers! Pin
Otomoto2005-Aug-05 5:11
Otomoto2005-Aug-05 5:11 
GeneralRe: This won't block spammers! Pin
Sean Michael Murphy5-Aug-05 10:10
Sean Michael Murphy5-Aug-05 10:10 
GeneralRe: This won't block spammers! Pin
NormDroid6-Aug-05 9:10
professionalNormDroid6-Aug-05 9:10 
GeneralRe: This won't block spammers! Pin
Anonymous5-Aug-05 5:11
Anonymous5-Aug-05 5:11 

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.