利用IHttpHandler做防盗链
public class HttpHandler : IHttpHandler
{
public bool IsReusable {
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
//throw new NotImplementedException();
//获取上次请求 的地址
Uri LastUrl = context.Request.UrlReferrer;
//获取本次请求 的地址
Uri benciUrl = context.Request.Url;
//判断是否为盗链
if (LastUrl.Host!=benciUrl.Host||LastUrl.Port!=benciUrl.Port)
{
//盗链
//返回一张警示图片
string errorPath = context.Request.PhysicalApplicationPath + "images/activity1.jpg";
//返回(响应)给客户端
context.Response.WriteFile(errorPath);
}
else
{
context.Response.WriteFile(context.Request.PhysicalPath);
}
}
}
}
配置文件
<!--配置文件-->
<system.webServer>
<handlers>
<!--不让盗取的类型-->
<add name="demo" verb="*" path="images/*.jpg" type="第三节.HttpHandler"/>
<add name="demo2" verb="*" path="images/*.png" type="第三节.HttpHandler"/>
</handlers>
</system.webServer>