我這裏用的圖片被盜舉例子
一個正常的網頁
<%@ 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>
效果圖
找不到的那個圖片,圖片無法顯示的那個,是一張圖片,我在類裏面改的,如果是盜取的話,就給他一個找不到的圖片