5,696,038 members and growing! (11,014 online)
Email Password   helpLost your password?
Web Development » Web Security » Security     Intermediate

Simple Captcha with ASP.NET

By Oguz Altuncu

Simple text to image generator to block spammers inserting data to your database, with ASP.NET.
C#, Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 5 Aug 2005
Updated: 5 Aug 2005
Views: 22,061
Bookmarked: 25 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
9 votes for this Article.
Popularity: 1.87 Rating: 1.96 out of 5
5 votes, 55.6%
1
0 votes, 0.0%
2
1 vote, 11.1%
3
2 votes, 22.2%
4
1 vote, 11.1%
5

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.
    <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:
    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:
    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

About the Author

Oguz Altuncu



Location: Canada Canada

Other popular Web Security articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralThis won't block spammers!memberJJF0075:55 5 Aug '05  
GeneralRe: This won't block spammers!memberlordofthexings6:11 5 Aug '05  
GeneralRe: This won't block spammers!memberSean Michael Murphy11:10 5 Aug '05  
GeneralRe: This won't block spammers!membernorm.net10:10 6 Aug '05  
GeneralRe: This won't block spammers!sussAnonymous6:11 5 Aug '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Aug 2005
Editor: Smitha Vijayan
Copyright 2005 by Oguz Altuncu
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project