使用验证码效验
功能概述
当用户登录的时候,在其他登录信息正确的前提下,输入验证码与系统自动生成的验证码相匹配则安然登录。
主要功能:
Ø 生成验证码
Ø 效验验证码
基本原理:
根据一定的规则生成随机的5为字符(由0—9的数字和A—Z的字母组成),并写入Session。验证的时候再从Session中取出进行比较。
前提知识:
关于ashx文件
本质:缺少html文件的aspx文件。
使用场景:
适合生成动态的图像或文本。
ashx输出作为页面元素img的背景(属性src的值,eg:<img src="../Handler/WaterMark.ashx" id="vimg" alt="" onclick="change()" />)
.ashx文件有个缺点,他处理控件的回发事件非常麻烦,比如说如果用它来生成DataGrid的列表也不是不行,但是处理数据的回发,需要一些.aspx页的功能,只有自己手动处理这些功能。所以,一般使用.ashx,用来输出一些不需要回发处理的项目即可。
程序设计
1 <%@ WebHandler Language="C#" Class="WaterMark" %>
2
3 using System;
4 using System.Web;
5 using System.Drawing;
6 using System.Drawing.Drawing2D;
7 using System.Web.SessionState;
8
9 public class WaterMark : IHttpHandler,IRequiresSessionState{
10 //使用Session时必须实现IRequiresSessionState接口,并引入命名空间System.Web.SessionState
11 public void ProcessRequest (HttpContext context) {
12 string checkCode = GenCode(5);
13 context.Session["Code"] = checkCode;
14 System.Drawing.Bitmap image = new System.Drawing.Bitmap(70, 22);
15 Graphics g = Graphics.FromImage(image);
16 try
17 {
18 Random random = new Random();
19
20 g.Clear(Color.White);
21
22 int i;
23 for (i = 0; i < 25; i++)
24 {
25 int x1 = random.Next(image.Width);
26 int x2 = random.Next(image.Width);
27 int y1 = random.Next(image.Height);
28 int y2 = random.Next(image.Height);
29 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
30 }
31
32 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
33 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, true);
34 g.DrawString(checkCode, font, brush, 2, 2);
35
36 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
37 System.IO.MemoryStream ms = new System.IO.MemoryStream();
38 image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
39 context.Response.ClearContent();
40 context.Response.ContentType = "image/png";
41 context.Response.BinaryWrite(ms.ToArray());
42 }
43 finally {
44 g.Dispose();
45 image.Dispose();
46 }
47 }
48
49 //产生随机字符串
50 private string GenCode(int num) {
51 string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
52 char[] chastr = str.ToCharArray();
53 string code = "";
54 Random rd = new Random();
55 int i;
56 for (i = 0; i < num;i++ )
57 {
58 code += str.Substring(rd.Next(0, str.Length), 1);
59 }
60 return code;
61 }
62 public bool IsReusable {
63 get {
64 return false;
65 }
66 }
67
68 }
2
3 using System;
4 using System.Web;
5 using System.Drawing;
6 using System.Drawing.Drawing2D;
7 using System.Web.SessionState;
8
9 public class WaterMark : IHttpHandler,IRequiresSessionState{
10 //使用Session时必须实现IRequiresSessionState接口,并引入命名空间System.Web.SessionState
11 public void ProcessRequest (HttpContext context) {
12 string checkCode = GenCode(5);
13 context.Session["Code"] = checkCode;
14 System.Drawing.Bitmap image = new System.Drawing.Bitmap(70, 22);
15 Graphics g = Graphics.FromImage(image);
16 try
17 {
18 Random random = new Random();
19
20 g.Clear(Color.White);
21
22 int i;
23 for (i = 0; i < 25; i++)
24 {
25 int x1 = random.Next(image.Width);
26 int x2 = random.Next(image.Width);
27 int y1 = random.Next(image.Height);
28 int y2 = random.Next(image.Height);
29 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
30 }
31
32 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
33 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, true);
34 g.DrawString(checkCode, font, brush, 2, 2);
35
36 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
37 System.IO.MemoryStream ms = new System.IO.MemoryStream();
38 image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
39 context.Response.ClearContent();
40 context.Response.ContentType = "image/png";
41 context.Response.BinaryWrite(ms.ToArray());
42 }
43 finally {
44 g.Dispose();
45 image.Dispose();
46 }
47 }
48
49 //产生随机字符串
50 private string GenCode(int num) {
51 string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
52 char[] chastr = str.ToCharArray();
53 string code = "";
54 Random rd = new Random();
55 int i;
56 for (i = 0; i < num;i++ )
57 {
58 code += str.Substring(rd.Next(0, str.Length), 1);
59 }
60 return code;
61 }
62 public bool IsReusable {
63 get {
64 return false;
65 }
66 }
67
68 }
源代码下载:download
扩展阅读: