ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)

<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/46860.html" frameborder="0" width="468" scrolling="no" height="60"></iframe>
学习整理了一下
(一).功能
用HttpHandler实现图片验证码
ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
(二).代码如下
1. 处理程序文件 ValidateImageHandler.ashx代码如下
1 @WebHandlerLanguage="C#"Class="ValidateImageHandler"%>
2
3usingSystem;
4usingSystem.Web;
5usingSystem.Web.SessionState;
6usingSystem.Drawing;
7usingSystem.Drawing.Imaging;
8usingSystem.Text;
9
10///<summary></summary>
11///ValidateImageHandler生成网站验证码功能
12///
13publicclassValidateImageHandler:IHttpHandler,IRequiresSessionState
14{
15intintLength=5;//长度
16stringstrIdentify="Identify";//随机字串存储键值,以便存储到Session中
17publicValidateImageHandler()
18{
19}
20
21///<summary></summary>
22///生成验证图片核心代码
23///
24///
25publicvoidProcessRequest(HttpContexthc)
26{
27//设置输出流图片格式
28hc.Response.ContentType="image/gif";
29
30Bitmapb=newBitmap(200,60);
31Graphicsg=Graphics.FromImage(b);
32g.FillRectangle(newSolidBrush(Color.YellowGreen),0,0,200,60);
33Fontfont=newFont(FontFamily.GenericSerif,48,FontStyle.Bold,GraphicsUnit.Pixel);
34Randomr=newRandom();
35
36//合法随机显示字符列表
37stringstrLetters="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
38StringBuilders=newStringBuilder();
39
40//将随机生成的字符串绘制到图片上
41for(inti=0;iintLength;i++)
42{
43s.Append(strLetters.Substring(r.Next(0,strLetters.Length-1),1));
44g.DrawString(s[s.Length-1].ToString(),font,newSolidBrush(Color.Blue),i*38,r.Next(0,15));
45}
46
47//生成干扰线条
48Penpen=newPen(newSolidBrush(Color.Blue),2);
49for(inti=0;i10;i++)
50{
51g.DrawLine(pen,newPoint(r.Next(0,199),r.Next(0,59)),newPoint(r.Next(0,199),r.Next(0,59)));
52}
53b.Save(hc.Response.OutputStream,ImageFormat.Gif);
54hc.Session[strIdentify]=s.ToString();//先保存在Session中,验证与用户输入是否一致
55hc.Response.End();
56
57}
58
59///<summary></summary>
60///表示此类实例是否可以被多个请求共用(重用可以提高性能)
61///
62publicboolIsReusable
63{
64get
65{
66returntrue;
67}
68}
69}
70

2. 前台页面代码

1 asp:LoginID="Login1"runat="server"BackColor="#EFF3FB"BorderColor="#B5C7DE"BorderPadding="4"BorderStyle="Solid"BorderWidth="1px"Font-Names="Verdana"Font-Size="0.8em"ForeColor="#333333"OnAuthenticate="Login1_Authenticate">
2TitleTextStyleBackColor="#507CD1"Font-Bold="True"Font-Size="0.9em"ForeColor="White"/>
3InstructionTextStyleFont-Italic="True"ForeColor="Black"/>
4TextBoxStyleFont-Size="0.8em"/>
5LoginButtonStyleBackColor="White"BorderColor="#507CD1"BorderStyle="Solid"BorderWidth="1px"
6Font-Names="Verdana"Font-Size="0.8em"ForeColor="#284E98"/>
7LayoutTemplate>
8tableborder="0"cellpadding="4"cellspacing="0"style="border-collapse:collapse">
9tr>
10tdstyle="width:292px">
11tableborder="0"cellpadding="0">
12tr>
13tdalign="center"colspan="2"style="font-weight:bold;font-size:0.9em;color:white;
14background-color:#507cd1">
15登录td>
16tr>
17tr>
18tdalign="left"style="width:84px;height:31px;">
19asp:LabelID="UserNameLabel"runat="server"AssociatedControlID="UserName">用户名:asp:Label>td>
20tdstyle="height:31px;width:215px;">
21asp:TextBoxID="UserName"runat="server"Font-Size="0.8em"Width="113px">asp:TextBox>
22asp:RequiredFieldValidatorID="UserNameRequired"runat="server"ControlToValidate="UserName"
23ErrorMessage="必须填写“用户名”。"ToolTip="必须填写“用户名”。"ValidationGroup="Login1">*asp:RequiredFieldValidator>
24td>
25tr>
26tr>
27tdalign="left"style="width:84px">
28asp:LabelID="PasswordLabel"runat="server"AssociatedControlID="Password">密码:asp:Label>td>
29tdstyle="width:215px">
30asp:TextBoxID="Password"runat="server"Font-Size="0.8em"TextMode="Password">asp:TextBox>
31asp:RequiredFieldValidatorID="PasswordRequired"runat="server"ControlToValidate="Password"
32ErrorMessage="必须填写“密码”。"ToolTip="必须填写“密码”。"ValidationGroup="Login1">*asp:RequiredFieldValidator>
33td>
34tr>
35tr>
36tdstyle="width:84px;height:4px;"align="left">
37验证码:td>
38tdvalign="middle"style="height:31px;width:215px;"align="left">
39asp:TextBoxID="TextBox1"runat="server"Font-Size="0.8em"TextMode="Password">asp:TextBox>&nbsp;
40
td >
41 tr >
42 tr>
43tdalign="left"colspan="2"style="color:red">
44asp:CheckBoxID="RememberMe"runat="server"Text="下次记住我。"/>&nbsp;td>
45tr>
46tr>
47tdalign="right"colspan="2">
48asp:ButtonID="LoginButton"runat="server"BackColor="White"BorderColor="#507CD1"
49BorderStyle="Solid"BorderWidth="1px"CommandName="Login"Font-Names="Verdana"
50Font-Size="0.8em"ForeColor="#284E98"Text="登录"ValidationGroup="Login1"/>
51td>
52tr>
53table>
54td>
55tr>
56table>
57LayoutTemplate>
58
59asp:Login>
60

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

因为这里不需要注册
1 httpHandlers>
2 addverb="*"path="*.asah"type="System.Web.UI.SimpleHandlerFactory"/>
3 httpHandlers >
4

注意:

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

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

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

(三).示例代码下载


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1475140


你可能感兴趣的:(.net,Web,UI,asp.net,asp)