Simple Captcha Using C#

Abstract

This will show you how to create a simple captcha which you can play with to rotate add more effect etc.

I was trying to find a solution to block spammers adding data to our 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;
//' 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(); }
}
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";
} }
4) Finally make sure you add the related System libraries,
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
in front of the class and this is it.
Hope this helps. Regards
http://www.ottawatutors.com


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today make data protection a must-have, as we live in a data driven society. The digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join eVault Chief Technology …

  • Mobile is introducing sweeping changes throughout your workplace. As a senior stakeholder driving mobile initiatives in your organization, you may be lost in a sea of technologies and claims from vendors promising rapid delivery of applications to your employees, customers, and partners. To help explain some of the topics you will need to be aware of, and to separate the must-haves from the nice-to-haves, this reference guide can help you with applying a mobile strategy in the context of application …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds