HttpHandler实现验证码

1:创建VerificationCode类(一般处理程序)继承于接口:IHttpHandler, IRequiresSessionState

namespace WebApplication5._4
{
    /// <summary>
    /// VerificationCode 的摘要说明
    /// </summary>
    public class VerificationCode : IHttpHandler, IRequiresSessionState  //继承于接口:IHttpHandler, IRequiresSessionState
    {

        private Random rand = new Random();  //产生一个随机数
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            string word = "1234567890abcdefg";
            string numStr = null;
            for (int i = 0; i < 5; i++)
            {
                numStr += word[rand.Next(0, word.Length)];
            }
            context.Session["vcode"] = numStr.ToLower();
            CreateImage(context, numStr);
        }

        private void CreateImage(HttpContext context, string checkCode)
        {
            int iwidth = checkCode.Length * 13;
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 22);
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.White);
            //定义颜色
            Color[] colors = { Color.Black, Color.Red, Color.Blue, Color.Green };
            //定义字体
            string[] fonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };

            Random rand = new Random();
            //随机输出噪点
            for (int i = 0; i < 50; i++)
            {
                int x = rand.Next(image.Width);
                int y = rand.Next(image.Height);
                g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
            }
            //输出不同字体和颜色的验证码字符
            for (int i = 0; i < checkCode.Length; i++)
            {
                int cindex = rand.Next(4);
                int findex = rand.Next(5);

                Font f = new Font(fonts[findex], 10, FontStyle.Bold);
                Brush b = new SolidBrush(colors[cindex]);

                int ii = 4;
                if ((i + 2) % 2 == 0)
                {
                    ii = 2;
                }
                g.DrawString(checkCode.Substring(i, 1), f, b, 2 + (i * 12), ii);
            }
            //画一个边框
            g.DrawRectangle(new Pen(ColorTranslator.FromHtml("#CCCCCC"), 0), 0, 0, image.Width - 1, image.Height - 1);
            //输出到浏览器
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            context.Response.ClearContent();
            context.Response.ContentType = "image/gif";
            context.Response.BinaryWrite(ms.ToArray());
            g.Dispose();
            image.Dispose();
        }
    }
}
2:创建Index.aspx窗体(前端样式) 登录成功后跳转界面(默认账号admin 密码123456)
<form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>账号:</td>
                    <td><asp:TextBox ID="txtAccount" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td><asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>验证码:</td>
                    <td>
                        <asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
                        <asp:Image ID="Image1" runat="server" imageUrl="~/VerificationCode.ashx"/>
                        <asp:LinkButton ID="LinkButton1" runat="server">刷新</asp:LinkButton>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Button ID="btnSubmit" runat="server" Text="提交" onclick="btnSubmit_Click"/>
                    </td>
                </tr>
            </table>
        </div>
    </form>
onclick点击事件:
            string vcode = Session["vcode"].ToString();
            if (txtAccount.Text == "admin" && txtPwd.Text == "123456" && txtCode.Text == vcode)
{ Response.Redirect(
"#"); //自己随便写一个登录成功的跳转界面 }

 

 

 




 

posted on 2022-04-19 17:58  钟硕cool  阅读(33)  评论(0编辑  收藏  举报

导航