asp. net combined with HttpHandler to achieve image anti hotlinking

  • 2020-05-10 17:57:04
  • OfStack

 
#region IHttpHandler  Members of the  

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 >

Related articles: