综合(盗链、水印、验证码)

一、HttpModule:经过多个HttpModule,层层处理

HttpHandler:请求的终点,最终的处理

1.首先创建一个网站,然后添加两个ASP.NET页面,并创建一个HttpModule类

namespace WebApplication1

{

    public class HttpModelu : IHttpHandler

    {

        public bool IsReusable

        {

            get

            {

                return true;

            }

        }

        public void ProcessRequest(HttpContext context)

        {

            Uri lastUrl = context.Request.UrlReferrer;

            Uri currenUrl = context.Request.Url;

            if (lastUrl.Host != currenUrl.Host || lastUrl.Port != currenUrl.Port)

            {

                string errorImagePath = context.Request.PhysicalApplicationPath + "Error/04.jpg";

                context.Response.WriteFile(errorImagePath);

            }

            else

            {

                context.Response.WriteFile(context.Request.PhysicalPath);

            }

        }

    }

}

2.配置信息,实现了HttpModule类,还需要在web.confing中配置,在<configuration>节点下添加如下代码:

<system.webServer>

    <handlers>

      <add verb="*" path="Images/*" type="WebApplication1.HttpModelu" name="plink"/>

    </handlers>

  </system.webServer>

3.创建站点

1)创建被盗站点。首先创建包含了jpg图片资源的网站,然后添加一个aspx页面,插入本网站提供的三张图片,代码如下:

<div>

            <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/01.jpg" />

            <asp:Image ID="Image2" runat="server" ImageUrl="~/Images/02.jpg" />

            <asp:Image ID="Image3" runat="server" ImageUrl="~/Images/03.jpg" />

        </div>

2)创建盗链站点。创建第二个网点,添加aspx页面,引用第一个网站提供的三张图片,代码如下:

<div>

            <asp:Image ID="Image1" runat="server" ImageUrl="http://localhost:61014/Images/01.jpg" />

            <asp:Image ID="Image2" runat="server" ImageUrl="http://localhost:61014/Images/02.jpg"/>

            <asp:Image ID="Image3" runat="server" ImageUrl="http://localhost:61014/Images/03.jpg"/>

        </div>


二、图片水印

1.创建第一个网站,准备其图片资源

在网站中添加TestHandel类,继承IHttpHandler接口,编写加水印的功能,代码如下:

namespace WebApplication3

{

    public class TestHandel : IHttpHandler

    {

        private const string WATERMARK_URL = "~/Images/01.jpg";

        private const string DEFAULTIME_URL = "~/Error/04.jpg";

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

        public void ProcessRequest(HttpContext context)

        {

            string filePath = context.Request.PhysicalPath;

            System.Drawing.Image Cover;

            if (File.Exists(filePath))

            {

                Cover = Image.FromFile(filePath);

                Image watermark = Image.FromFile(context.Server.MapPath(WATERMARK_URL));

                Graphics G = Graphics.FromImage(Cover);

                G.DrawImage(watermark, new Rectangle(Cover.Width - watermark.Width,

                    Cover.Height - watermark.Height, watermark.Width, watermark.Height), 0,

                    0, watermark.Width, watermark.Height, GraphicsUnit.Pixel);

                G.Dispose();

                watermark.Dispose();

            }

            else

            {

                Cover = Image.FromFile(context.Request.MapPath(DEFAULTIME_URL));

            }

            context.Response.ContentType = "Images/jpeg";

            Cover.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

            Cover.Dispose();

            context.Response.End();

        }

    }

}

2.在Web.coning中配置实现当请求图片资源时由TestHandel类处理,配置如下:

<system.webServer>

    <handlers>

      <add verb="*" path="Images/*.jpg" type="WebApplication3.TestHandel" name="handerl1"/>

    </handlers>

  </system.webServer>

3.添加Web页面,展示图片,部分代码如下:

<div>

            <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/02.jpg" />

            <asp:Image ID="Image2" runat="server" ImageUrl="~/Images/03.jpg" />

        </div>


三、验证码

1.创建一个网站,并在网站中添加VerificationCode 类,继承IHttpHandler与IRequiresSessionState接口,代码如下:

namespace WebApplication3

{

    public class VerificationCode : IHttpHandler,IRequiresSessionState

    {

        private Random RandomSeed = new Random();

        public bool IsReusable => false;

        public void ProcessRequest(HttpContext context)

        {

            string strWord = "3456789QWERTYUPAS2DFGHKXCVBNM";

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

        {

            int iwidth = (int)(numStr.Length * 13);

            System.Drawing.Bitmap image = new System.Drawing.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 = { "宋体", "微软雅黑" };

            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 < numStr.Length; i++)

            {

                int cindex = rand.Next(7);

                int findex = rand.Next(5);

                Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);

                Brush b = new System.Drawing.SolidBrush(c[cindex]);

                int ii = 4;

                if ((i + 1) % 2 == 0)

                {

                    ii = 2;

                }

                g.DrawString(numStr.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、添加一个Web窗体,登录使用验证码实现登录验证功能

页面代码如下:

<body>

    <form id="form1" runat="server">

        <div>

            <table style="width:100%;" border="1" cellspacing="0" cellpadding="0">

                <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="VerificationCode.cs" />

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

</body>

后台部分代码(点击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("WebForm1.aspx");

                }

                else

                {

                    Label1.Text = "账号或者密码不正确!";

                }

            }

            else

            {

                Label1.Text = "验证码不正确";

            }

        }

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,589评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,615评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,933评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,976评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,999评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,775评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,474评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,359评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,854评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,007评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,146评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,826评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,484评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,029评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,153评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,420评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,107评论 2 356

推荐阅读更多精彩内容