Anti-leech settings under iis7.0, ASPnet anti-leech source code

Source: Internet
Author: User

Anti-leech settings under iis7.0 ,.
Category: Asp.net 4 reading comments (0) Add to favorites edit Delete
The common Asp.net anti-leech method is to write an httphandler class to implement the ihttphandler interface,CodeAs follows:

Public class imagehandler: ihttphandler {

Public void processrequest (httpcontext context ){
// Determine whether the image is referenced by a local website. If yes, the correct image is returned.
If (context. Request. urlreferrer! = NULL & context. Request. urlreferrer. Host. Equals ("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 );
// Send the information in the output cache to the client
Context. response. End ();
}
// If it is not a local reference, it is the image of the leeching site
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 + "IMGs/fdl.jpg ");
// Send the information in the output cache to the client
Context. response. End ();
}

}
 
Public bool isreusable {
Get {
Return false;
}
}

After the task is finished, add the following configuration in Web. config,

<Httphandlers>
<Add verb = "*" type = "imagehandler" Path = "*. jpg, *. jpeg" Validate = "false"/>
</Httphandlers>

It can be run locally, but it seems that the anti-leech is not accessible under iis7.0. Of course, running locally is also a problem (Environment: Asp. net4.0). iis7.0 can access anti-leech through the following methods:

Prerequisites: You must install isapi_rewrite3.

: Http://www.helicontech.com/download.htm

<System. webserver>
<Rewrite>
<Rules>
<Rule name = "forbid image">
<Match url = "\. (txt | Doc | GIF | JPG | JPEG | PNG | MP3 | FLV | SWF) $" ignorecase = "false"/>
<Conditions>
<Add input = "{http_referer}" pattern = "^ $" ignorecase = "false" negate = "true"/>
<Add input = "{http_referer}" pattern = "^ http: // (www \.)? Passji.com/.#$ "negate =" true "/>
</Conditions>

<Action type = "customresponse" statuscode = "403" statusreason = "forbidden" statusdescription = "forbidden"/>
</Rule>
</Rules>
</Rewrite>
</System. webserver>

If you want to display images such as "prohibit leeching" that you have prepared on the website of your image

<Action type = "customresponse" statuscode = "403" statusreason = "forbidden" statusdescription = "forbidden"/>
Modify:

<Action type = "Rewrite" url = "/IMGs/fbl.png"/>

IMGs/fbl.png can be modified based on your actual image location and name. After the test, the iis7.0 anti-leech can pass.

Another method is to write a configuration file with. htaccess suffix! Then import the configuration rules on IIS!

Rewritecond % {http_referer }! ^ $ [Nc]
Rewritecond % {http_referer }! Google.com [Nc]
Rewritecond % {http_referer }! Baidu.com [Nc]
Rewritecond % {http_referer }! Soso.com [Nc]
Rewritecond % {http_referer }! Bing.com [Nc]
Rewriterule. * \. (GIF | JPG | JPEG | BMP | PNG) $ http://www.wovane.com/fbl.png [R, NC, l]

Reference: http://wenku.baidu.com/view/6121f509581b6bd97f19eace.html

 

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.