基于HttpHandler对象实现网页登录验证码功能

Posted In Sec

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于HttpHandler对象实现网页登录验证码功能相关的知识,希望对你有一定的参考价值。

创建Asp.Net Web程序建一个Handler类调用IHttpHandler接口

public class handler : IHttpHandler,IRequiresSessionState
    {
        public bool IsReusable => false;

        private Random RandomSeed = new Random();
        public void ProcessRequest(HttpContext context)
        {
            //string uri = context.Request.PhysicalPath;
            //Bitmap bitmap = new Bitmap(uri);
            //Graphics gra = Graphics.FromImage(bitmap);
            //gra.DrawString("xxx", new Font("楷书", 20, FontStyle.Bold), Brushes.Red, new Point(700, 800));
            //gra.Flush();
            //bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            //context.Response.WriteFile(uri);
            string strWord = "23456789qwertyuiopasdfghjklzxcvbnm";
            string NumStr = null;
            for(int i = 0; i < 5; i++)
            {
                NumStr += strWord[RandomSeed.Next(0, strWord.Length)];
            }
            context.Session["vcode"] = NumStr.ToLower();
            CreateImages(context, NumStr);

        }
        private void CreateImages(HttpContext context,string checkCode)
        {
            int iwidth = (int)(checkCode.Length * 13);
            Bitmap image = new Bitmap(iwidth, 22);
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.White);
            Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
            string[] font = { "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(7);
                int findex = rand.Next(5);
                Font f = new Font(font[findex], 10, FontStyle.Bold);
                Brush b = new SolidBrush(c[cindex]);
                int ii = 4;
                if ((i + 1) % 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);
            MemoryStream ms = new MemoryStream();
            image.Save(ms, ImageFormat.Jpeg);
            context.Response.ClearContent();
            context.Response.ContentType = "images/gif";
            context.Response.BinaryWrite(ms.ToArray());
            g.Dispose();
            image.Dispose();
            context.Response.BinaryWrite(ms.ToArray());
            g.Dispose();
            image.Dispose();
        }
        
    }

添加Web窗体.创建控件

       <div>
            <table>
                <tr>
                    <td>账号:</td>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

                    </td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

                    </td>
                </tr>
                <tr>
                    <td>验证码:</td>
                    <td>
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                        <asp:Image ID="Image1" runat="server" imageUrl="~/images/2025cbe68b27ef7051689ee2bd164546.jpeg"/>
                        <asp:LinkButton ID="LinkButton1" runat="server">刷新</asp:LinkButton>
                    </td>
                </tr>
            </table>
            <asp:Button ID="Button1" runat="server" Text="提交"  Style="margin-left:50%;margin-top:2%" OnClick="Button1_Click"/>
        </div>

在登录Button控件的点击事件中编写逻辑代码

     protected void Button1_Click(object sender, EventArgs e)
        {
            string account = this.TextBox1.Text;
            string password = this.TextBox2.Text;
            string code = this.TextBox3.Text;
            if (Session["vcode"].ToString() == code.ToLower())
            {
                if (account == "admin" && password == "123456")
                {
                    Response.Redirect("index.aspx");
                }
                else
                {
                    Label1.Text = "账号密码不正确";
                }
            }
            else
            {
                Label1.Text = "验证码不正确";
            }
        }

 

以上是关于基于HttpHandler对象实现网页登录验证码功能的主要内容,如果未能解决你的问题,请参考以下文章

基于HttpHandler对象实现网页登录验证码功能

基于HttpHandler对象实现网页登录验证码功能

基于HttpHandler对象实现网页防盗链功能

基于HttpHandler对象实现网页防盗链功能

基于HttpHandler对象实现网页防盗链功能

基于HttpHandler对象实现网页防盗链功能