当前位置: 首页 > news >正文

asp.net文件防盗链

URLRewriter实现

可以参考下面的文章

代码

.net framework

新建asp.net framework的web项目,新建AntiTheftChainHandler

using System.Web;namespace AntiTheftChainStu01.Handler
{public class AntiTheftChainHandler : IHttpHandler{public bool IsReusable => true;/// <summary>/// jpg文件防盗链/// </summary>/// <param name="context"></param>public void ProcessRequest(HttpContext context){string FileName = context.Server.MapPath(context.Request.FilePath);string notFoundFile = context.Server.MapPath("/404.jpg");if (context.Request.UrlReferrer ==null|| context.Request.UrlReferrer.Host == null){context.Response.ContentType = "image/JPEG";context.Response.WriteFile(notFoundFile);}else{//此处的可填你网站的域名,因为我这里采用的是本机演示,故使用localhostif (context.Request.UrlReferrer.Host.IndexOf("localhost") != -1){context.Response.ContentType = "image/JPEG";context.Response.WriteFile(FileName);}else{context.Response.ContentType = "image/JPEG";context.Response.WriteFile(notFoundFile);}}}}
}

修改web.config

<system.webServer><!-- 确保所有请求都经过ASP.NET的管道处理 --><modules runAllManagedModulesForAllRequests="true" /><handlers><add name="jpgHandler" path="*.jpg" verb="*" type="AntiTheftChainStu01.Handler.AntiTheftChainHandler,AntiTheftChainStu01" /></handlers><!--设置默认起始页面--><defaultDocument><files><clear /><add value="Index.aspx" /></files></defaultDocument>
</system.webServer>

新建测试的html

<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><img src="http://localhost:63099/image/imgs/002.jpg" alt="">
</body>
</html>

最后效果如下
在这里插入图片描述
微信公众号图片也是实现的类似的效果
在这里插入图片描述

.net core

新建中间件RefuseStealingMiddleWare

namespace AntiTheftChainStu02
{public class RefuseStealingMiddleWare{private readonly RequestDelegate _next;public RefuseStealingMiddleWare(RequestDelegate next){_next = next;}public async Task Invoke(HttpContext context){string url = context.Request.Path.Value;if (!url.Contains(".jpg")){await _next(context);//走正常流程return;}string urlReferrer = context.Request.Headers["Referer"];if (string.IsNullOrWhiteSpace(urlReferrer))//直接访问{await this.SetForbiddenImage(context);//返回404图片}else if (!urlReferrer.Contains("localhost"))//非当前域名{await this.SetForbiddenImage(context);//返回404图片}else{await _next(context);//走正常流程}}/// <summary>/// 设置拒绝图片/// </summary>/// <param name="context"></param>/// <returns></returns>private async Task SetForbiddenImage(HttpContext context){string defaultImagePath = "wwwroot/404.jpg";string path = Path.Combine(Directory.GetCurrentDirectory(), defaultImagePath);FileStream fs = File.OpenRead(path);byte[] bytes = new byte[fs.Length];await fs.ReadAsync(bytes, 0, bytes.Length);await context.Response.Body.WriteAsync(bytes, 0, bytes.Length);}}
}

修改Program.cs

namespace AntiTheftChainStu02
{public class Program{public static void Main(string[] args){var builder = WebApplication.CreateBuilder(args);var app = builder.Build();app.UseMiddleware<RefuseStealingMiddleWare>();app.UseDefaultFiles();app.UseStaticFiles();app.Run();}}
}

新建index.html

<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><img src="https://localhost:7229/image/imgs/001.jpg" alt="">
</body>
</html>

在这里插入图片描述

参考

https://cloud.tencent.com/developer/article/1459550
https://www.cnblogs.com/mbskys/articles/633043.html
https://blog.csdn.net/net_programmer1/article/details/136627093
https://blog.csdn.net/weixin_42084199/article/details/110874803

http://www.lryc.cn/news/478334.html

相关文章:

  • 【日志】力扣58.最后一个单词的长度//14.最长公共前缀//28. 找出字符串中第一个匹配项的下标
  • 华为杯”第十五届中国研究生数学建模竞赛-B题:光传送网建模与价值评估(续)
  • android 使用xml设置背景图片和圆角
  • 数据结构,问题 E: 表达式括号匹配
  • 国家宠物美容师职业技能等级评价(高级)理论考试题
  • Spring挖掘:(AOP篇)
  • 十四届蓝桥杯STEMA考试Python真题试卷第二套第四题
  • 单元测试怎么做
  • 移动应用开发 实验二:标准身高计算器
  • 金华迪加现场大屏互动系统 mobile.do.php 任意文件上传漏洞复现
  • 使用 pd.ExcelWriter 创建多工作表 Excel 文件的详细教程
  • 驱动-----dht11温湿度传感器
  • Docker 基础命令简介
  • 嵌入式开发之静态库和共享库
  • 关于npm源的切换及相关操作
  • vue前端sku实现
  • 使用Vue3和Vue2进行开发的区别
  • 爬虫入门urllib 和 request(二)
  • 【大数据学习 | HBASE】hbase的整体架构
  • 群控系统服务端开发模式-应用开发-个人资料
  • openssl生成加密,公钥实现非对称加密
  • [CKS] K8S Admission Set Up
  • 前端学习Day13 CSS盒子的定位(固定定位篇“附练习”)
  • Tomcat 启动卡住,日志显示 At least one JAR was scanned for TLDs yet contained no TLDs.
  • 计算机网络:网络层 —— 移动 IP 技术
  • useCrudSchemas
  • SpringBoot3集成Junit5
  • 【EMNLP2024】阿里云人工智能平台 PAI 多篇论文入选 EMNLP2024
  • Spark的Shuffle过程
  • Java+Swing可视化图像处理软件