ASP.NET中“字母和数字混合的验证码”详解

“字母跟数字混合的验证码技术”:
验证码技术是网站开发过程中比较重要的技术,可以防止非法人员利用注册机或者登陆工具来攻击我们的网站。废话少说,切入正题。主要思路是:引用Using System.Drawing命名空间,利用Graphics的FromImage方法创建一个画布,同时设置画布的宽和高,然后通过Graphics类的DrawString方法随机生成的字符串绘制到画布中,绘制验证码的同时,在画布中利用SetPixel方法绘制一些色点,从而防止非法人员利用机器人来进行登陆。当我们绘制验证码完毕后,在需要验证码的页面中利用Image空间将其显示出来,Image控件显示验证码的HTML源码设置如下:
<asp:Image ID="Image1" src="CheckCode.aspx" runat="server" Height="20px" Width="80px"/>
注意:当然加入src属性时程序会提示src不是元素Image的有效属性,这不影响程序的执行。由于验证码存放在客户端机器的Cookie中,因此在调用的时候可以将文本框中的数据和Cookie中的数据进行对照。从客户端的机器中取出Cookie值如下代码:HttpCookie cookie=Request.Cookies["CheckCode"];
举个例子:主要有:输入验证码txtCode,显示验证码Image1,提交/取消 btnOk/btnCancel主要代码如下:
(1)在绘制验证码之前,必须生成随机字符串。代码如下:

 1    private string GenerateCheckCode()
 2    {
 3        int number;
 4        char code;
 5        string checkCode = String.Empty;
 6        Random random = new Random();
 7        for (int i = 0; i < 4; i++)
 8        {
 9            number = random.Next();
10
11            if (number % 2 == 0)
12                code = (char)('0' + (char)(number % 10));
13            else
14                code = (char)('A' + (char)(number % 26));
15
16            checkCode += code.ToString();
17        }

18
19        Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
20        return checkCode;
21    }

22
23

代码比较简单我就不在解释。
(2)字符串生成后,接下来就是将该字符串绘制成图片显示出来。代码如下:

 1   private void CreateCheckCodeImage(string checkCode)
 2    {
 3        if (checkCode == null || checkCode.Trim() == String.Empty)
 4            return;
 5        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);                 Graphics g = Graphics.FromImage(image);
 6        try
 7        {
 8            //生成随机生成器
 9            Random random = new Random();
10            //清空图片背景色
11            g.Clear(Color.White);
12            //画图片的背景噪音线
13            for (int i = 0; i < 2; i++)
14            {
15                int x1 = random.Next(image.Width);
16                int x2 = random.Next(image.Width);
17                int y1 = random.Next(image.Height);
18                int y2 = random.Next(image.Height);
19                g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
20            }

21
22            Font font = new System.Drawing.Font("Arial"12, (System.Drawing.FontStyle.Bold));
23            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(00, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2ftrue);
24            g.DrawString(checkCode, font, brush, 22);
25            //画图片的前景噪音点
26            for (int i = 0; i < 100; i++)
27            {
28                int x = random.Next(image.Width);
29                int y = random.Next(image.Height);
30                image.SetPixel(x, y, Color.FromArgb(random.Next()));
31            }

32
33            //画图片的边框线
34            g.DrawRectangle(new Pen(Color.Silver), 00, image.Width - 1, image.Height - 1);
35            System.IO.MemoryStream ms = new System.IO.MemoryStream();
36            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
37            Response.ClearContent();
38            Response.ContentType = "image/Gif";
39            Response.BinaryWrite(ms.ToArray());
40        }

41        finally
42        {
43            g.Dispose();
44            image.Dispose();
45        }

46    }

47
48

以上带都是我运行过的,同时参照MSDN文档,大家放心使用。如有错误请联系我QQ:1264373,谢谢!
最好引用比较的代码是:

 1    protected void Button1_Click(object sender, EventArgs e)
 2    {
 3        HttpCookie cookie = Request.Cookies["CheckCode"];
 4        if (cookie.Value == this.txtCode.Text.Trim())
 5        {
 6            Response.Write("<script>alert('验证码正确!')</script>");
 7        }

 8        else
 9        {
10            Response.Write("<script>alert('验证码错误!')</script>"); 
11        }

12    }

posted on 2007-12-17 22:25  CodeShark  阅读(1115)  评论(0编辑  收藏  举报

导航