How to use ASP. NET anti-leech Protection

Source: Internet
Author: User

Anti-leeching technology can effectively solve the problems of low website traffic but high pressure. Now we will take the .jpg file as an example to illustrate how to implement anti-leech Technology in Asp.net.

Create an Asp.net website and add two image files: right.jpg, error.jpg, and right.jpg, which are the images shown on the current site, and error.jpg, which are the error images when other websites access the website.

Default. aspx place the following code:

<Asp: Image id = "image1" runat = "server" Height = "179px" imageurl = "~ /Right.jpg "width =" 197px "/>

Create a handler. CS class with the following code:

Using system;
Using system. Web;

Public class handler: ihttphandler {

Public void processrequest (httpcontext context ){

Context. response. expires = 0;
Context. response. Clear ();
Context. response. contenttype = "image/jpg ";

If (context. Request. urlreferrer. Host = "localhost ")
{

Context. response. writefile (context. Request. physicalpath );
Context. response. End ();
}
Else
{
Context. response. writefile (context. Request. physicalapplicationpath + "error.jpg ");
Context. response. End ();
}
}

Public bool isreusable {
Get {
Return true;
}
}

}

Add the following code to the Web. config node:

<Add verb = "*" Path = "*. jpg" type = "handler"/>

Debug the program, and the picture will be displayed as normal.

Give "localhost" in the Code if (context. Request. urlreferrer. Host = "localhost") to another host, and the program will display error.jpg

In this way, we have achieved what we call the image anti-leech technology.

Now, how can this effect be displayed in IIS.

Choose website> right-click Properties> virtual directory> Configuration> ing> Add

Press add to select an executable file:

C: \ windows \ Microsoft. NET \ framework \ v2.0.50727 \ aspnet_isapi.dll

In this way, your website also implements anti-leech technology.

 

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.