All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET中多国语言的实现_[Asp.Net教程]
PHP has encountered an Access Violation解决方法总结_[PHP教程]
初学PHP指导:php.ini 配置详细选项_[PHP教程]
javascript:世界上误解最深的语言_JavaScript技术_编程技术
解析SQL Server 2008中的新语句:MERGE_[SQL Server教程]
总结PHP网站编程8个初级技巧_[PHP教程]
一条语句查询数据库中所以表的信息_[SQL,Server教程]
系统安全之SA弱口令带来的安全隐患_[SQL Server教程]
跳出封装剖析ASP.NET脚本回调的原理_.net资料_编程技术
Delphi使用ODBC连接SQServer 2000数据库
Reading number is top 10 pictures
Take you to walk into the most true north Korea rural2
2012 national geographic daily picture3
壮丽的云彩2
迷人的靓女
这才是真正的人体艺术4
8090后结婚的各种XX事
Fierce! China's special forces training the devil1
Players in the eyes of a perfect love2
Rendez-vous Sleep with actress, three days to earn 600000
这才是真正的人体艺术2
Download software ranking
Unix video tutorial3
VC++6.0培训教程
jdk1.5
Boxer's Top ten classic battle7
Boxer vs Yellow4
C++教程第四版
Unix video tutorial15
Sora aoi 120 minutes
1400篇各类破解文章
Twenty piece of palm leaf
delv published in(发表于) 2014/1/24 9:02:34 Edit(编辑)
ASP.NET,2.0,HttpHandler实现生成图片验证码(示例代码下载)_[Asp.Net教程]

ASP.NET,2.0,HttpHandler实现生成图片验证码(示例代码下载)_[Asp.Net教程]

ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)_[Asp.Net教程]


学习整理了一下
(一).功能
用HttpHandler实现图片验证码

(二).代码如下
1. 处理程序文件 ValidateImageHandler.ashx代码如下


1 <%@ WebHandler Language="C#" Class="ValidateImageHandler" %>
2
3 using System;
4 using System.Web;
5 using System.Web.SessionState;
6 using System.Drawing;
7 using System.Drawing.Imaging;
8 using System.Text;
9
10 ///
11 /// ValidateImageHandler 生成网站验证码功能
12 ///

13 public class ValidateImageHandler : IHttpHandler, IRequiresSessionState
14 {
15 int intLength = 5; //长度
16 string strIdentify = "Identify"; //随机字串存储键值,以便存储到Session中
17 public ValidateImageHandler()
18 {
19 }
20
21 ///
22 /// 生成验证图片核心代码
23 ///

24 ///
25 public void ProcessRequest(HttpContext hc)
26 {
27 //设置输出流图片格式
28 hc.Response.ContentType = "image/gif";
29
30 Bitmap b = new Bitmap(200, 60);
31 Graphics g = Graphics.FromImage(b);
32 g.FillRectangle(new SolidBrush(Color.YellowGreen), 0, 0, 200, 60);
33 Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
34 Random r = new Random();
35
36 //合法随机显示字符列表
37 string strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
38 StringBuilder s = new StringBuilder();
39
40 //将随机生成的字符串绘制到图片上
41 for (int i = 0; i < intLength; i++)
42 {
43 s.Append(strLetters.Substring(r.Next(0, strLetters.Length - 1), 1));
44 g.DrawString(s[s.Length - 1].ToString(), font, new SolidBrush(Color.Blue), i * 38, r.Next(0, 15));
45 }
46
47 //生成干扰线条
48 Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
49 for (int i = 0; i < 10; i++)
50 {
51 g.DrawLine(pen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
52 }
53 b.Save(hc.Response.OutputStream, ImageFormat.Gif);
54 hc.Session[strIdentify] = s.ToString(); //先保存在Session中,验证与用户输入是否一致
55 hc.Response.End();
56
57 }
58
59 ///
60 /// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
61 ///

62 public bool IsReusable
63 {
64 get
65 {
66 return true;
67 }
68 }
69 }
70

2. 前台页面代码


1
2
3
4
5 6 Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" />
7
8

9

10

11

12

13
14 background-color: #507cd1">
15 登录

16

17

18

19 用户名:

20

21
22 23 ErrorMessage="必须填写“用户名”。" ToolTip="必须填写“用户名”。" ValidationGroup="Login1">*
24

25

26

27

28 密码:

29

30
31 32 ErrorMessage="必须填写“密码”。" ToolTip="必须填写“密码”。" ValidationGroup="Login1">*
33

34

35

36

37 验证码:

38

39 &nbsp;
40


41

42

43

44 &nbsp;

45

46

47

48 49 BorderStyle="Solid" BorderWidth="1px" CommandName="Login" Font-Names="Verdana"
50 Font-Size="0.8em" ForeColor="#284E98" Text="登录" ValidationGroup="Login1" />
51

52

53

54

55

56

57

58
59

60

3.这里因为使用的是默认 *.asah处理文件类型,在machine.config文件中已经有此类型的默认注册,


因为这里不需要注册
1
2
3
4

注意:


1.再注册一下也不会出错,会覆盖machine.config文件配置


2.如果在同一个配置文件中注册多次,默认后者也会覆盖前者.


3.如果其它格式(系统默认没有注册)的,务必要在Web.config文件中注册一下.



(三).示例代码下载
http://www.cnblogs.com/Files/ChengKing/ValidateImageHttpHandler.rar

来源:cnblogs






添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.