用HttpHandler实现图片验证码

<%@ 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 /// <summary>
11 /// ValidateImageHandler 生成网站验证码功能
12 /// </summary>
13 public class ValidateImageHandler : IHttpHandler, IRequiresSessionState
14 {
15     int intLength = 5;               //长度
16     string strIdentify = "Identify"//随机字串存储键值,以便存储到Session中
17     public ValidateImageHandler()
18     {        
19     }
20 
21     /// <summary>
22     ///  生成验证图片核心代码
23     /// </summary>
24     /// <param name="hc"></param>
25     public void ProcessRequest(HttpContext hc)
26     {
27         //设置输出流图片格式
28         hc.Response.ContentType = "image/gif";
29         
30         Bitmap b = new Bitmap(20060);
31         Graphics g = Graphics.FromImage(b);
32         g.FillRectangle(new SolidBrush(Color.YellowGreen), 0020060);
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(015));
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(0199), r.Next(059)), new Point(r.Next(0199), r.Next(059)));
52         }
53         b.Save(hc.Response.OutputStream, ImageFormat.Gif);
54         hc.Session[strIdentify] = s.ToString(); //先保存在Session中,验证与用户输入是否一致
55         hc.Response.End();
56    
57     }
58     
59     /// <summary>
60     /// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
61     /// </summary>
62     public bool IsReusable
63     {
64         get
65         {
66             return true;
67         }
68     }
69 }

前台代码 单击验证码 可更换图片

<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">
            <TitleTextStyle BackColor ="#507CD1" Font-Bold ="True" Font-Size ="0.9em" ForeColor ="White" />
            <InstructionTextStyle Font-Italic ="True"  ForeColor ="Black" />
            <TextBoxStyle Font-Size="0.8em" />
            <LoginButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px"
                Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" />
            <LayoutTemplate>
                <table border="0" cellpadding="4" cellspacing="0" style="border-collapse: collapse">
                    <tr>
                        <td>
                            <table border="0" cellpadding="0">
                                <tr>
                                    <td align="center" colspan="2" style="font-weight: bold; font-size: 0.9em; color: white;
                                        background-color: #507cd1">
                                        登录</td>
                                </tr>
                                <tr>
                                    <td align="left" style ="width:84px;height:31px;">
                                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名:</asp:Label></td>
                                    <td style="height:31px; width:215px;">
                                        <asp:TextBox ID="UserName" runat="server" Font-Size="0.8em" Width = "113px"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                            ErrorMessage="必须填写“用户名”。" ToolTip="必须填写“用户名”。" ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="left" style ="width:84px">
                                        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密码:</asp:Label></td>
                                    <td style ="width:215px">
                                        <asp:TextBox ID="Password" runat="server" Font-Size="0.8em" TextMode="Password"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                                            ErrorMessage="必须填写“密码”。" ToolTip="必须填写“密码”。" ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                <td style="width:84px; height:4px;" align ="left">验证码</td>
                                <td valign ="middle" style ="height:31px; width:215px" align="left" >
                                    <asp:TextBox ID ="TextBox1" runat ="server" Font-Size ="0.8em" TextMode ="password" ></asp:TextBox>&nbsp;
                                    <img width ="100px" height="25px" src ="ValidateImageHandler.ashx"  onclick ="this.src='ValidateImageHandler.ashx?duty='+Math.random()"/>
                               
                                </td>
                                </tr>
                                <tr>
                                   
                               
                                    <td colspan="2" style="height: 20px">
                                        <asp:CheckBox ID="RememberMe" runat="server" Text="下次记住我。" />
                                                                        </tr>
                                <tr>
                                   
                             
                                    <td align="right" colspan="2" style="height: 17px">
                                        <asp:Button ID="LoginButton" runat="server" BackColor="White" BorderColor="#507CD1"
                                            BorderStyle="Solid" BorderWidth="1px" CommandName="Login" Font-Names="Verdana"
                                            Font-Size="0.8em" ForeColor="#284E98" Text="登录" ValidationGroup="Login1" />
                                    </td>
                                </tr>
                                <tr>
                                <td align="right" colspan="2" style="color: red">
                                      
                                        <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                    </td>
                               
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            
        </asp:Login>

70  


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值