ASP.NET防止自己网站的资源被盗(通过IHttpHandler 带样例说明)

我这里用的图片被盗举例子

一个正常的网页

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="StolenDemo.aspx.cs" Inherits="Stolen.StolenDemo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <img src="Images/adv1.jpg" /><img src="Images/adv2.jpg" /><img src="Images/adv3.jpg" />
        </div>
    </form>
</body>
</html>

模拟一个盗用网站的网页
localhost是我另一个项目的,这里服务器不方便弄,就直接新建项目

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="StealDemo.aspx.cs" Inherits="steal.StealDemo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
              <img src="https://localhost:44353/Images/adv1.jpg" />
              <img src="https://localhost:44353/Images/adv2.jpg" />
              <img src="https://localhost:44353/Images/adv3.jpg" />
        </div>
    </form>
</body>
</html>

没有任何防盗的操作效果图,

在这里插入图片描述
防盗措施:

建一个类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Stolen
{
    public class prevention : IHttpHandler
    {
        public bool IsReusable =>true;

        public void ProcessRequest(HttpContext context)
        {
            //上一次的uri与这一次的Uri看看是不是host和port是不是相同,如果不相同说明是被盗了
            Uri pre = context.Request.UrlReferrer;
            Uri cur = context.Request.Url;
            if (pre.Host != cur.Host || pre.Port != cur.Port)
            {
                string errorPath=context.Request.PhysicalApplicationPath+ "Error/default.jpg";
                context.Response.WriteFile(errorPath);
            }
            else
            {
                context.Response.WriteFile(context.Request.PhysicalPath);
            }
        }
    }
}

找到配置文件添加代码
在这里插入图片描述
path是防盗的范围,type是那个类的路径:

也就是命名空间 .(点)类名

<system.webServer>
    <handlers>
      <add verb="*" name="preventLink" path="Images/*.jpg" type="Stolen.prevention"/>
    </handlers>
  </system.webServer>

效果图
找不到的那个图片,图片无法显示的那个,是一张图片,我在类里面改的,如果是盗取的话,就给他一个找不到的图片
在这里插入图片描述

  • 22
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 34
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值