首页 > 技术文章 > Asp.net 图片文件防盗链介绍

wangsai 2014-11-21 16:52 原文

想要实现文件放盗链的功能

首先添加一个全局文件 Global.asax

在 Application_BeginRequest中我们可以判断Http报文头中的UrlReferre是否来源本站。

if (HttpContext.Current.Request.UrlReferrer != null)
{
if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) && HttpContext.Current.Request.UrlReferrer.Host != "localhost")
{
HttpContext.Current.Response.WriteFile(HttpContext.Current.Server.MapPath("~/jzdl.jpg"));
HttpContext.Current.Response.End();
}
}

推荐阅读