当前位置:Gxlcms > asp.net > asp.net下结合HttpHandler实现图片防盗链

asp.net下结合HttpHandler实现图片防盗链

时间:2021-07-01 10:21:17 帮助过:30人阅读

代码如下:

#region IHttpHandler 成员

bool IHttpHandler.IsReusable
{
get { return true; }
}

void IHttpHandler.ProcessRequest(HttpContext context)
{
string FileName = context.Server.MapPath(context.Request.FilePath);
if (context.Request.UrlReferrer.Host == null)
{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/no.jpg");
}
else
{
if (context.Request.UrlReferrer.Host.IndexOf("mydomain.com") > 0)
{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile(FileName);
}
else
{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("no/jpg");
}
}
}

#endregion

<httpHandlers>
<add verb="*" path="*.jpg" type="JpgHandler, MyDll" />
</httpHandlers>

人气教程排行