利用HttpHandler实现验证码
span style="font-size: 12pt">输出图片并保存验证码在Session里面。

ValidateCode.ashx
1
<%@ WebHandler Language="C#" Class="ValidateCode" %>
2
3
using System;
4
using System.Web;
5
using System.Web.SessionState;
6
7
/**//// <summary>
8
/// 验证码程序
9
/// </summary>
10
public class ValidateCode : IHttpHandler, IRequiresSessionState
11

{
12
/**//// <summary>
13
/// 入口
14
/// </summary>
15
/// <param name="context"></param>
16
public void ProcessRequest(HttpContext context)
17
{
18
string checkCode = CreateRandomCode(4);
19
context.Session["Code"] = checkCode;
20
CreateImage(checkCode, context);
21
}
22
23
public bool IsReusable
24
{
25
get
26
{
27
return false;
28
}
29
}
30
31
/**//// <summary>
32
/// 产生验证码
33
/// </summary>
34
/// <param name="codeCount"></param>
35
/// <returns></returns>
36
private string CreateRandomCode(int codeCount)
37
{
38
//验证码中的出现的字符,避免了一些容易混淆的字符。
39
string allChar = "3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,T,U,W,X,Y";
40
string[] allCharArray = allChar.Split(',');
41
string randomCode = "";
42
int temp = -1;
43
44
Random rand = new Random();
45
for (int i = 0; i < codeCount; i++)
46
{
47
if (temp != -1)
48
{
49
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
50
}
51
int t = rand.Next(allCharArray.Length);
52
if (temp == t)
53
{
54
return CreateRandomCode(codeCount);
55
}
56
temp = t;
57
randomCode += allCharArray[t];
58
}
59
return randomCode;
60
}
61
62
/**//// <summary>
63
/// 创建图片
64
/// </summary>
65
/// <param name="checkCode"></param>
66
/// <param name="context"></param>
67
private void CreateImage(string checkCode, HttpContext context)
68
{
69
int iwidth = (int)(checkCode.Length * 12);
70
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
71
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
72
g.Clear(System.Drawing.Color.White);
73
//定义颜色
74
System.Drawing.Color[] c =
{ System.Drawing.Color.DimGray, System.Drawing.Color.DimGray, System.Drawing.Color.DimGray };
75
//定义字体
76
string[] font =
{ "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
77
Random rand = new Random();
78
//随机输出噪点
79
for (int i = 0; i < 50; i++)
80
{
81
int x = rand.Next(image.Width);
82
int y = rand.Next(image.Height);
83
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.LightGray, 0), x, y, 1, 1);
84
}
85
86
//输出不同字体和颜色的验证码字符
87
88
for (int i = 0; i < checkCode.Length; i++)
89
{
90
int cindex = rand.Next(3);
91
int findex = rand.Next(1);
92
93
System.Drawing.Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);
94
System.Drawing.Brush b = new System.Drawing.SolidBrush(c[cindex]);
95
int ii = 4;
96
if ((i + 1) % 2 == 0)
97
{
98
ii = 2;
99
}
100
g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 10), ii);
101
}
102
//画一个边框
103
104
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.DarkGray, 0), 0, 0, image.Width - 1, image.Height - 1);
105
106
//输出到浏览器
107
System.IO.MemoryStream ms = new System.IO.MemoryStream();
108
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
109
context.Response.ClearContent();
110
context.Response.ContentType = "image/Jpeg";
111
context.Response.BinaryWrite(ms.ToArray());
112
g.Dispose();
113
image.Dispose();
114
}
115
116
}
117
注意:
必须继承System.Web.SessionState.IRequiresSessionState接口,才能实现Session读写!
System.Web.SessionState的一些接口
IReadOnlySessionState 指定目标 HTTP 处理程序只需要具有对会话状态值的读访问权限。这是一个标记接口,没有任何方法。
IRequiresSessionState 指定目标 HTTP 处理程序需要对会话状态值具有读写访问权。这是一个标记接口,没有任何方法。
1
<%@ WebHandler Language="C#" Class="ValidateCode" %>2

3
using System;4
using System.Web;5
using System.Web.SessionState;6

7

/**//// <summary>8
/// 验证码程序9
/// </summary>10
public class ValidateCode : IHttpHandler, IRequiresSessionState11


{12

/**//// <summary>13
/// 入口14
/// </summary>15
/// <param name="context"></param>16
public void ProcessRequest(HttpContext context)17

{18
string checkCode = CreateRandomCode(4);19
context.Session["Code"] = checkCode;20
CreateImage(checkCode, context);21
}22

23
public bool IsReusable24

{25
get26

{27
return false;28
}29
}30

31

/**//// <summary>32
/// 产生验证码33
/// </summary>34
/// <param name="codeCount"></param>35
/// <returns></returns>36
private string CreateRandomCode(int codeCount)37

{38
//验证码中的出现的字符,避免了一些容易混淆的字符。39
string allChar = "3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,T,U,W,X,Y";40
string[] allCharArray = allChar.Split(',');41
string randomCode = "";42
int temp = -1;43

44
Random rand = new Random();45
for (int i = 0; i < codeCount; i++)46

{47
if (temp != -1)48

{49
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));50
}51
int t = rand.Next(allCharArray.Length);52
if (temp == t)53

{54
return CreateRandomCode(codeCount);55
}56
temp = t;57
randomCode += allCharArray[t];58
}59
return randomCode;60
}61

62

/**//// <summary>63
/// 创建图片64
/// </summary>65
/// <param name="checkCode"></param>66
/// <param name="context"></param>67
private void CreateImage(string checkCode, HttpContext context)68

{69
int iwidth = (int)(checkCode.Length * 12);70
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);71
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);72
g.Clear(System.Drawing.Color.White);73
//定义颜色74

System.Drawing.Color[] c =
{ System.Drawing.Color.DimGray, System.Drawing.Color.DimGray, System.Drawing.Color.DimGray };75
//定义字体 76

string[] font =
{ "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };77
Random rand = new Random();78
//随机输出噪点79
for (int i = 0; i < 50; i++)80

{81
int x = rand.Next(image.Width);82
int y = rand.Next(image.Height);83
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.LightGray, 0), x, y, 1, 1);84
}85

86
//输出不同字体和颜色的验证码字符87

88
for (int i = 0; i < checkCode.Length; i++)89

{90
int cindex = rand.Next(3);91
int findex = rand.Next(1);92

93
System.Drawing.Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);94
System.Drawing.Brush b = new System.Drawing.SolidBrush(c[cindex]);95
int ii = 4;96
if ((i + 1) % 2 == 0)97

{98
ii = 2;99
}100
g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 10), ii);101
}102
//画一个边框103

104
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.DarkGray, 0), 0, 0, image.Width - 1, image.Height - 1);105

106
//输出到浏览器107
System.IO.MemoryStream ms = new System.IO.MemoryStream();108
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);109
context.Response.ClearContent();110
context.Response.ContentType = "image/Jpeg";111
context.Response.BinaryWrite(ms.ToArray());112
g.Dispose();113
image.Dispose();114
}115

116
}117

--------------------------------------------------
页面调用<img src="ashx文件地址">
判断验证码是否正确
Session["Code"].ToString()==输入的字符
浙公网安备 33010602011771号