(转)验证码
1
CheckCode.aspx.cs
2
/*********************************************************************/
3
using System;
4
using System.Data;
5
using System.Configuration;
6
using System.Collections;
7
using System.Web;
8
using System.Web.Security;
9
using System.Web.UI;
10
using System.Web.UI.WebControls;
11
using System.Web.UI.WebControls.WebParts;
12
using System.Web.UI.HtmlControls;
13
using System.Drawing;
14
public partial class CheckCode : System.Web.UI.Page
15
{
16
private void Page_Load(object sender, System.EventArgs e)
17
{
18
this.CreateCheckCodeImage(GenerateCheckCode());
19
}
20
private string GenerateCheckCode()
21
{
22
int number;
23
char code;
24
string checkCode = String.Empty;
25
System.Random random = new Random();
26
for (int i = 0; i < 5; i++)
27
{
28
number = random.Next();
29
if (number % 2 == 0)
30
code = (char)('0' + (char)(number % 10));
31
else
32
code = (char)('A' + (char)(number % 26));
33
checkCode += code.ToString();
34
}
35
Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
36
return checkCode;
37
}
38
private void CreateCheckCodeImage(string checkCode)
39
{
40
if (checkCode == null || checkCode.Trim() == String.Empty)
41
return;
42
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
43
Graphics g = Graphics.FromImage(image);
44
try
45
{
46
//生成随机生成器
47
Random random = new Random();
48
//清空图片背景色
49
g.Clear(Color.White);
50
//画图片的背景噪音线
51
for (int i = 0; i < 25; i++)
52
{
53
int x1 = random.Next(image.Width);
54
int x2 = random.Next(image.Width);
55
int y1 = random.Next(image.Height);
56
int y2 = random.Next(image.Height);
57![](/Images/OutliningIndicators/InBlock.gif)
58
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
59
}
60
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
61
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);
62
g.DrawString(checkCode, font, brush, 2, 2);
63
//画图片的前景噪音点
64
for (int i = 0; i < 100; i++)
65
{
66
int x = random.Next(image.Width);
67
int y = random.Next(image.Height);
68![](/Images/OutliningIndicators/InBlock.gif)
69
image.SetPixel(x, y, Color.FromArgb(random.Next()));
70
}
71
//画图片的边框线
72
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
73
System.IO.MemoryStream ms = new System.IO.MemoryStream();
74
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
75
Response.ClearContent();
76
Response.ContentType = "image/Gif";
77
Response.BinaryWrite(ms.ToArray());
78
}
79
finally
80
{
81
g.Dispose();
82
image.Dispose();
83
}
84
}
85
}
登录页面中使用“<ImageButton>” 这个 HTML 元素来显示生成的验证码图片:<asp:ImageButton ID="ImageButton1" ImageUrl="CheckCode.aspx" runat="server"/>
在登录页面的登录按钮的处理事件中使用以下代码判断验证码:
private void btnLogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(Request.Cookies["CheckCode"] == null)
{
lblMessage.Text = "您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
lblMessage.Visible = true;
return;
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/InBlock.gif)
70
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
84
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
85
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
登录页面中使用“<ImageButton>” 这个 HTML 元素来显示生成的验证码图片:<asp:ImageButton ID="ImageButton1" ImageUrl="CheckCode.aspx" runat="server"/>
在登录页面的登录按钮的处理事件中使用以下代码判断验证码:
private void btnLogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(Request.Cookies["CheckCode"] == null)
{
lblMessage.Text = "您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
lblMessage.Visible = true;
return;
}
if(String.Compare(Request.Cookies["CheckCode"].Value, txtCheckCode.Text, true) != 0)
{
lblMessage.Text = "验证码错误,请输入正确的验证码。";
lblMessage.Visible = true;
return;
}
}
http://www.spdevelop.com/forum/Answer.aspx?QuestionId=38