When reading a book, I found a method for implementing Asp.net 2.0 to prevent leeching. The actual principle is to use the httphandler module in IIS for processing. Because, for example
Normally, IIS only processes files such as ASP and aspx, but does not process images such as IIS and jpg. Below is a brief summary.
1. Create a website (vs.net 2005) and add handler. ashx to process the file and process the HTTP request,CodeAs follows:
Program code
<% @ Webhandler Language = "C #" class = "handler" %>
Using system;
Using system. Web;
Public class handler: ihttphandler {
Public void processrequest (httpcontext context ){
// Determine whether it is a local reference. If yes, the correct image is returned to the client.
// Here, we use the page information recorded in the HTTP request.
// For a website, you can change "localhost" to the website address.
If (context. Request. urlreferrer. Host = "localhost ")
{
// Set the file expiration time in the client buffer to 0, that is, it expires immediately.
Context. response. expires = 0;
// Clear the output cache opened by the server for this session
Context. response. Clear ();
// Obtain the file type
Context. response. contenttype = "image/jpg ";
// Write the request file to the output Cache
Context. response. writefile (context. Request. physicalpath );
// Send the information in the output cache to the client
Context. response. End ();
}
// If it is not a local reference, it is a leeching reference and an error image is returned to the client.
Else
{
// Set the file expiration time in the client buffer to 0, that is, it expires immediately.
Context. response. expires = 0;
// Clear the output cache opened by the server for this session
Context. response. Clear ();
// Obtain the file type
Context. response. contenttype = "image/jpg ";
// Write image files with special report errors to the output Cache
Context. response. writefile (context. Request. physicalapplicationpath + "error.jpg ");
// Send the information in the output cache to the client
Context. response. End ();
}
}
Public bool isreusable
{
Get
{
Return true;
}
}
}
Create a handler. CS file and put it in the app_code directory. The content of the file is handler. aschx,
2. Configure the following in Web. config:
Program code
<Httphandlers>
<Add verb = "*" Path = "*. jpg" type = "handler"/>
</Httphandlers>
3. Process in IIS
In the "configuration" of the default website in IISProgramIng
The mapped executable file is "vs. net2005 installation path \ aspnet_isapi.dll" and the extension is ". jpg!