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

news/2024/7/3 10:47:05
 
学习整理了一下
(一).功能
 
      用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  < asp:Login ID = " 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 " >
 2               < TitleTextStyle BackColor = " #507CD1 "  Font - Bold = " True "  Font - Size = " 0.9em "  ForeColor = " White "   />
 3               < InstructionTextStyle Font - Italic = " True "  ForeColor = " Black "   />
 4               < TextBoxStyle Font - Size = " 0.8em "   />
 5               < LoginButtonStyle BackColor = " White "  BorderColor = " #507CD1 "  BorderStyle = " Solid "  BorderWidth = " 1px "
 6                  Font - Names = " Verdana "  Font - Size = " 0.8em "  ForeColor = " #284E98 "   />
 7               < LayoutTemplate >
 8                   < table border = " 0 "  cellpadding = " 4 "  cellspacing = " 0 "  style = " border-collapse: collapse " >
 9                       < tr >
10                           < td style = " width: 292px " >
11                               < table border = " 0 "  cellpadding = " 0 " >
12                                   < tr >
13                                       < td align = " center "  colspan = " 2 "  style = " font-weight: bold; font-size: 0.9em; color: white;
14                                          background - color: #507cd1 " >
15                                          登录 td>
16                                  tr>
17                                 <tr>
18                                     <td align="left" style="width: 84px; height: 31px;">
19                                         <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名: asp:Label> td>
20                                     <td style="height: 31px; width: 215px;">
21                                         <asp:TextBox ID="UserName" runat="server" Font-Size="0.8em" Width="113px"> asp:TextBox>
22                                         <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
23                                             ErrorMessage="必须填写“用户名”。" ToolTip="必须填写“用户名”。" ValidationGroup="Login1">* asp:RequiredFieldValidator>
24                                      td>
25                                  tr>
26                                 <tr>
27                                     <td align="left" style="width: 84px">
28                                         <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密码: asp:Label> td>
29                                     <td style="width: 215px">
30                                         <asp:TextBox ID="Password" runat="server" Font-Size="0.8em" TextMode="Password"> asp:TextBox>
31                                         <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
32                                             ErrorMessage="必须填写“密码”。" ToolTip="必须填写“密码”。" ValidationGroup="Login1">* asp:RequiredFieldValidator>
33                                      td>
34                                  tr>
35                                 <tr>
36                                     <td style="width: 84px; height: 4px;" align="left">
37                                         验证码: td>
38                                     <td valign="middle" style="height: 31px; width: 215px;" align="left">
39                                         <asp:TextBox ID="TextBox1" runat="server" Font-Size="0.8em" TextMode="Password"> asp:TextBox>&nbsp;
40                                         
                                 
                                        td>
41                                  tr>
42                                 <tr>
43                                     <td align="left" colspan="2" style="color: red">
44                                         <asp:CheckBox ID="RememberMe" runat="server" Text="下次记住我。" />&nbsp; td>
45                                  tr>
46                                 <tr>
47                                     <td align="right" colspan="2">
48                                         <asp:Button ID="LoginButton" runat="server" BackColor="White" BorderColor="#507CD1"
49                                             BorderStyle="Solid" BorderWidth="1px" CommandName="Login" Font-Names="Verdana"
50                                             Font-Size="0.8em" ForeColor="#284E98" Text="登录" ValidationGroup="Login1" />
51                                      td>
52                                  tr>
53                              table>
54                          td>
55                      tr>
56                  table>
57              LayoutTemplate>
58         
59          asp:Login>
60 

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

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

 注意:

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

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

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

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




http://www.niftyadmin.cn/n/3658051.html

相关文章

NLP学习笔记(3)

NMT&#xff08;nerual machine translation&#xff09;任务经常被描述为一个encode-decode过程&#xff0c;输入序列利用RNN进行encode产生一个编码&#xff08;包含句子的语义信息&#xff09;&#xff0c;将这个编码送进decode的RNN产生target的语言序列&#xff0c;这就完…

NLP学习笔记(4)

不要在RNN的水平方向使用dropout&#xff08;虽然有人这么做&#xff0c;但是是用特殊的方法的&#xff09;&#xff0c;但是经常在垂直方向使用dropout初始化参数的时候&#xff0c;LSTM的forget gate的bias给1或2确保不会一开始就遗忘&#xff0c;然后其它的参数要初始化为很…

Asp.net 2.0 自定义控件开发[创建自定义HeaderRow的GridView控件][示例代码下载]

(一). 概述1. 做了个GridViewExtend自定义控件, 通过注册扩展自定义表头事件, 可以任意设置Header的格式.2. 易重用代码, 只要将ExtendGridView.dll 文件添加到项目引用即可. 实现原理&#xff1a;增加一个自定义事件&#xff0c;并将GridView头(HeaderRow)引用作为事件参数…

NLP学习笔记(5)

传统的语音识别模型需要建立一个generative模型&#xff0c;从语言模型中产生单词序列&#xff0c;然后产生tokens&#xff08;音标&#xff09;序列&#xff0c;然后产生语音序列&#xff08;时域或频域的声音序列&#xff09;&#xff0c;然后是计算得到一些特征&#xff08;…

深入浅出话窗体(一)——窗体事件模型(上)

深入浅出话窗体&#xff08;一&#xff09;——窗体事件模型&#xff08;上&#xff09;作者&#xff1a;CSDN 刘铁猛小序&#xff1a;工作中最大的挑战并不是那些Mission Impossible&#xff0c;而是你需要一边保持安静、平衡的心态以专注于工作&#xff0c;一边对抗公司体制、…

NLP学习笔记(6)

我们希望把多个单词组成的短语同样在词向量的空间中找到对应的向量&#xff0c;当然你也可以在不同的空间中&#xff0c;但是如果在同一空间中是有好处的 &#xff0c;我可以利用该空间捕捉到短语与单词之间意思的相似度&#xff0c;比如the man on the snowboard 和 snowboard…

Programming C# 4th. Edition 中文/英文版对照阅读体验

Programming C# 4th Edition 中文版/英文版 对照阅读体验大概是耐心所至的缘故&#xff0c;我那几篇冠以“深入浅出C#”的文章在网络里被广泛转载——这当然是好事&#xff01;有更多的朋友通过我的介绍认识并喜欢上了C#这门可爱的语言。广泛转载的Side Effect就是我的邮箱时常…

C# 3.0 语言定义文档(微软官方版)正式发布!

C# 3.0 语言定义文档&#xff08;微软官方版&#xff09;正式发布如果想精通一门编程语言&#xff0c;你就需要像律师研究法律条款一样来研究这门语言的定义文档&#xff08;Language Specification&#xff0c;简称LangSpec&#xff09;。静悄悄地&#xff0c;C# 3.0语言定义文…