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.
- 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�.
- 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;
Font objFont = new Font("Arial", 8, FontStyle.Bold);
string randomStr="";
int[] myIntArray = new int[5] ;
int x;
Random autoRand = new Random();
for (x=0;x<5;x++)
{
myIntArray[x] = System.Convert.ToInt32 (autoRand.Next(0,9));
randomStr+= (myIntArray[x].ToString ());
}
Session.Add("randomStr",randomStr);
objGraphics.DrawString(randomStr, objFont, Brushes.White, 3, 3);
Response.ContentType = "image/GIF";
objBMP.Save(Response.OutputStream, ImageFormat.Gif);
objFont.Dispose();
objGraphics.Dispose();
objBMP.Dispose();
}
}
- 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 ()))
{
}
else
{
Label1.Text ="Please enter info correctly";
}
}
|
| | Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh) | FirstPrevNext |
|
 |
|
 |
All you do is to write some random chars out at the same formating and font. This will prevent maybe some beginners to get the code, but with just some simple coding algorithms its possible to get the wanted letters back from the image!
All spamm blocking image creators use differnt high, angels and fonts for sometimes each letter.
|
|
|
|
 |
|
 |
That's the simplest way to do it, I am sure it will prevent many bots. Thanks for the advice
|
|
|
|
 |
|
 |
You can make it a little harder on OCR attacks just by changing from a solid brush to a hatch brush. Instead of this:
objGraphics.DrawString(randomStr, objFont, Brushes.White, 3, 3);
Do this:
HatchBrush hb = new HatchBrush(HatchStyle.Sphere, Color.Red, Color.Wheat); objGraphics.DrawString(randomStr, objFont, hb, 3, 3);
Play with the HatchStyle enumeration. It chops up the characters a bit more, making them slightly harder to process via code.
Sean
|
|
|
|
 |
|
 |
Apply some transforms to the image and bingo, you've now it.
Blogless
|
|
|
|
 |
|
 |
totally right. even Captcha has been cracked http://www.captcha.net/
|
|
|
|
 |
|
|
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.