![]() |
Web Development »
Web Security »
Security
Intermediate
Simple Captcha with ASP.NETBy Oguz AltuncuSimple text to image generator to block spammers inserting data to your database, with ASP.NET. |
C#, Windows, .NET, ASP.NET, Visual Studio, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
I was trying to find a solution to block spammers adding data to the database, using C#. This is the quick solution I found.
<IMG height="30" alt="" src="Turing.aspx" width="80">
As you see, the source for the picture will be a file called �Turing.aspx�.
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();
}
}
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";
}
}| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 5 Aug 2005 Editor: Smitha Vijayan |
Copyright 2005 by Oguz Altuncu Everything else Copyright © CodeProject, 1999-2009 Web10 | Advertise on the Code Project |