Implement ASP. NET anti-leech protection [httphandler]
Sometimes we need to prevent other websites from directly referencing images in our system or downloading file links. You need to disable leeching! This function can be conveniently implemented in ASP. NET!
The following is an example of a simple leeching image. Create a new webapplcation. Refer image.
The default. aspx page is simple. Take an image as follows:
< Form ID = "Form1" Runat = "Server" >
< Div >
< IMG SRC = "Images/logo.jpg" />
</ Div >
</ Form >
Httphandler is used to solve the leeching problem.
Create a general process Program Handler1.ashx.Code And the annotations are as follows: Public Void Processrequest (httpcontext context)
{
// Determines whether an image is referenced by a local website. If yes, the correct image is returned.
If (Context. Request. urlreferrer. Host = " Localhost " )
{
// Set the Client Buffer expiration time to 0, that is, immediate expiration
Context. response. Expires = 0 ;
// Clear the output cache enabled by the server for this session
Context. response. Clear ();
// Set the output file type
Context. response. contenttype = " Image/jpg " ;
// Write the request file to the output cache.
Context. response. writefile (context. Request. physicalpath );
// Transmits the information in the output cache to the client.
Context. response. End ();
}
// If it is not a local reference, it is a leeching site image
Else
{
// Set the Client Buffer expiration time to 0, that is, immediate expiration
Context. response. Expires = 0 ;
// Clear the output cache enabled by the server for this session
Context. response. Clear ();
// Set the output file type
Context. response. contenttype = " Image/jpg " ;
// Write the request file to the output cache.
Context. response. writefile (context. Request. physicalapplicationpath + " Images/error.jpg " );
// Transmits the information in the output cache to the client.
Context. response. End ();
}
}
// Indicates whether the HTTP request can be processed
Public Bool Isreusable
{
Get
{
Return True;
}
}
this file is used to take over JPG images of HTTP requests. If it is accessed from the host localhost, it is allowed. Otherwise, an incorrect image is displayed!
this file has no effect, and must be stored on the web. configure the httphandler node in the config file as follows httphandlers >
Add verb =" * " path = ". jpg " type =" mynamespace. handler, mynamespace " />
httphandlers >
To test the startup
The image is displayed normally. If you change the access address to http: // 127.0.0.1: 2136/default. aspx, the effect is as follows:
However, this project is now published to IIS to run
The expected effect is not displayed because the request via IIS does not. JPG format. net engine, but directly returned to the user like static page html. In this case, we want the user to request. JPG can also be like. you can solve this problem by enabling IIS. Select this website
Right-click attribute
Click Configuration
Click Add
. Jpg. It is handed over to aspnet_isapi.dll for processing, so that the general handler handler1.ashx we write will be effective,
Maybe many people may ask, aren't we writing such a configuration in Web. config? Why is it ineffective. This is because iisdirectly loads the image and returns the result of a request with the suffix of .jpg. Here, it does not use aspnet_isapi.dll for processing, so it cannot even reach handler1.ashx. After this step is configured in IIS, it can be implemented!
You can also download anti-leech files based on the same principle ~ Similar principles ~
By using this method, we can also solve the problem that the website news is crawled by a spider program!
However, this method does not have no disadvantages. The first thing is to reduce the system performance! This depends on personal trade-offs!