使用HttpModule和HttpHandler添加水印,进行验证码验证

 

 

一、水印

1.先获取到图片

 string uri = context.Request.PhysicalPath;

2.添加画板

Bitmap bitmap = new Bitmap(uri);
            Graphics gra = Graphics.FromImage(bitmap);

3.为水印设置样式

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);

4.配置文件

<system.webServer>
    <handlers>
      <add name="handle" verb="*" type="WebApplication2.handler" path="images/*"/>
    </handlers>
  </system.webServer>

二、验证码

1.创建一个工程,在工程里添加HttpHandler类,继承IHttpHandler和IRequiresSeeionState接口

2.创建随机数对象

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

        private Random RandomSeed = new Random();

3.提供验证使用的字符,将验证码保存到Session中,为验证码添加样式

public void ProcessRequest(HttpContext context)
        {
            
            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);
            }

4.给一个空白框

g.DrawRectangle(new Pen(ColorTranslator.FromHtml("#cccccc"), 0), 0, 0, image.Width - 1, image.Height - 1);

5.输出到浏览器

 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();

6.Handler类里面代码完成,再添加一个窗体,实现登录验证功能

<form id="form1" runat="server">
        <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>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </form>

7.再编写逻辑代码。先绑定文本框的值,在进行登录验证

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 = "验证码不正确";
            }
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值