ASP.NET picture anti hotlinking implementation principle analysis

  • 2020-05-07 19:31:13
  • OfStack

So let me show you one way to put hotlinking on a picture
First, add an httpHandlers request. The WEBCONFIG configuration section is as follows:
< httpHandlers >
< add verb="*" path="*.jpg" type="myhandler,App_Code"/ >
< /httpHandlers > Note: under system.web, make no mistake!
Then add an class, named myhandler after IHttpHandler
The class prototype is as follows:
 
using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
public class myhandler : IHttpHandler 
{ 
public void 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("~/images/no.png");// Replaced picture  
} 
else 
{ 
if (context.Request.UrlReferrer.Host.IndexOf("localhost") > -1)// Here is your domain name  
{ 
context.Response.ContentType = "image/JPEG"; 
context.Response.WriteFile(FileName); 
} 
else 
{ 
context.Response.ContentType = "image/JPEG"; 
context.Response.WriteFile("~/images/no.png"); 
} 
} 
} 
public bool IsReusable 
{ 
get { return true; } 
} 
public myhandler() 
{ 
} 
} 

myhandler inherits from IHttpHandler and implements an url source to determine whether jpg pictures are hotlinking or not. In this class example, localhost can be used to modify localhost and images/ no.png parameters
The secondary method can also be applied to URL pseudo-static.
Next time we will talk about the picture and ASP.NET program server separation, welcome to visit this site.

Here is the supplement
The so-called URL static (under the declaration: here is a pseudo-static, real web page static do not say that you can use the simplest request to return response implementation, but it seems that over time the amount of data will be very large, so do not say) is the dynamic web page looks like a static 1. Such as: aspnet aspx? id=5 static: aspnet-5.html or /aspnet/5

SEO(search engine optimization), more in favor of static web pages, so web page static for professional SEOer or must master, I tell you the following 1 method to achieve in ASP.NET page static.

Give a DEMO below, download address: / / www ofstack. com codes / 23902. html


This DEMO implements the pseudo-static URL of the web page, and it redirects the web page by matching the regularity of url, which has an config in DEMO, < add virtualUrl="~/microsoft*.*" destinationUrl="~/default.aspx" / >
. That is to say the request microsoft html, microsoft shtml, microsoft. do will be mapped to default. aspx

This regular redirects URL to 1 page without any parameters. First, I will introduce how to implement the parameters. Next, I add this rule
< add virtualUrl="~/default-([0-9]*)" destinationUrl="~/default.aspx?id=$1" / >
The regular match, such URL (assuming http: / / www. xx. com/is your domain name), www. xx. com/microsoft - 123 mapped to www. xx. com/default aspx? id = 123
Then the parameter is passed

Multiple parameters can be used to achieve the following regular, you want to use what kind of static rules to write their own regular how to achieve, next I give a directory of URL pseudo-static rules
< add virtualUrl="~/default/([a-zA-Z0-9]*)/([a-zA-Z0-9]*)" destinationUrl="~/default.aspx?par1=$1 & par2=$2" / > Here's the thing to notice & (&)

Related articles: