Implement anti-leech protection in Asp.net 2.0

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.