Analysis on the implementation principles of ASP. NET image anti-leech protection

Source: Internet
Author: User

So I will introduce a method for leeching.
First, add httphandlers requests. The configuration section of webconfig is as follows:
<Httphandlers>
<Add verb = "*" Path = "*. jpg" type = "myhandler, app_code"/>
</Httphandlers> Note: Do not add any errors under system. Web!
Then add a class named myhandler to inherit from ihttphandler.
The class prototype is as follows: CopyCode The Code is as follows: using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public class myhandler: ihttphandler
{
Public void processrequest (httpcontext context)
{
String filename = context. server. mappath (context. Request. filepath );
If (context. Request. urlreferrer. Host = NULL)
{
Context. response. contenttype = "image/JPEG ";
Context. response. writefile ("~ /Images/no.png "); // replaced Image
}
Else
{
If (context. Request. urlreferrer. Host. indexof ("localhost")>-1) // here is your domain name
{
Context. response. contenttype = "image/JPEG ";
Context. response. writefile (filename );
}
Else
{
Context. response. contenttype = "image/JPEG ";
Context. response. writefile ("~ /Images/no.png ");
}
}
}
Public bool isreusable
{
Get {return true ;}
}
Public myhandler ()
{
}
}

Myhandler inherits from ihttphandler to determine whether the jpg image is leeched from the URL source. In this class, use localhost as an example to modify the localhost and images/no.png parameters.
The method can also be applied to URL pseudo-static.
Next chat and ASP. NETProgramYou are welcome to visit this site.

Below is a supplement
The so-called static URL (Declaration: here we are talking about pseudo-static. If the real static webpage is not mentioned, we can use the simplest request to return response, but it seems that the data volume will be huge over time, so I won't talk about it.) dynamic web pages look like static pages. For example, ASPnet. aspx? Static: aspnet-5.html or/ASPnet/5

SEO (Search Engine Optimization) prefers static Web pages. Therefore, static Web pages must be mastered by professional seoer. Below I will tell you a way to implement ASP.. net.

Here is a demo: http://www.jb51.net/codes/23902.html

This demo implements the pseudo-static Web page url. Its principle is to perform webpage redirection by matching the URL regular expression. This demo contains such a config, <add virtualurl = "~ /Microsoft *. * "destinationurl = "~ /Default. aspx "/>
It is also said that the requests for microsoft.html, microsoft.shtml, and Microsoft. Do will be mapped to default. aspx.

This regular expression redirects the URL to a page without parameters. I will first introduce how to implement the following parameters. I have added this rule below.
<Add virtualurl = "~ /Default-([0-9] *) "destinationurl = "~ /Default. aspx? Id = $1 "/>
This regular expression matches. In this URL (Suppose http://www.xx.com/is your domain name), www.xx.com/microsoft-123 maps to www.xx.com/default.aspx? Id = 123
The parameter is passed.

The following regular expressions can be used to transmit multiple parameters. If you want to use static rules, you can write the regular expressions yourself. Below I will provide a directory-based URL pseudo-static rule.
<Add virtualurl = "~ /Default/([a-zA-Z0-9] *)/([a-zA-Z0-9] *) "destinationurl = "~ /Default. aspx? Par1 = $1 & par2 = $2 "/> note that &(&)

Related Article

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.