Picture, file anti-theft chain program

Source: Internet
Author: User
Program | anti-theft chain

About the picture hotlinking this problem, after all, is the success of their own work, many people do not want others to steal it so easily. This feature is on many forums, probably because hotlinking has too much to act on.
Anti-hotlinking program is actually very simple, familiar with the ASP.net application lifecycle words can easily write a, the use of HttpModule in the BeginRequest event interception request on the OK, the rest of the work is filtered, and then filtered!
If you are not familiar with HttpModule, you can go to the MSDN lookup, Introduction very detailed, Address: Ms-help://ms. Vscc.v80/ms. Msdn.v80/ms. Visualstudio.v80.chs/dv_aspnetcon/html/f1d2910f-61d0-4541-8af8-c3c108ca351f.htm. There's no more nonsense.

1private void Application_BeginRequest (Object source, EventArgs E)
2{
3 HttpApplication application = (HttpApplication) source;
4 HttpContext context = Application. context;
5 bool Issafe = true; is legal link
6 string uri = context. Request.Url.AbsolutePath.ToLower ();
7 if (URI. LastIndexOf (".") > 0 && context. Request.urlreferrer!= null)
8 {
9 string exp = URI. Substring (URI. LastIndexOf ("."));
10//Here is to determine if the file suffix name is within the excluded file type list
one bool Ishas = ClassLibrary.RData.RString.StrIsIncUseSC (exp, config. Imgsafetype.split (' | '));
if (Ishas)
13 {
String domainoutter = context.    Request.UrlReferrer.Authority.ToLower (); Include Domain name and port
ArrayList Arry = Common.Cache.GetDomainValid ()//To take a system-defined legal domain name binding list
Issafe = Arry.    Contains (Domainoutter); Determines whether the current requested domain name is within a valid list
17}
18}
19//Below is the output of the illegal time, if there is a default alternative to the picture output, if not to generate a, format for. gif
if (!issafe)
21 {
Bitmap img = null;
Graphics g = null;
MemoryStream ms = NULL;
25
A try
27 {
A string picpath = ClassLibrary.RPath.GetFullDirectory ("Images/unlawful.gif");
if (file.exists (Picpath))
30 {
img = new Bitmap (Picpath, false);
32}
Or else
34 {
img = new Bitmap (64, 64);
g = Graphics.fromimage (IMG);
Panax Notoginseng g.clear (color.white);
Font f = new font ("Arial, Blackbody, Arial", 9,fontstyle.bold);
SolidBrush s = new SolidBrush (color.red);
g.DrawString (Resources.Message.LawlessLink, F, S, 1, 20);
The IMG. Save (Picpath, imageformat.gif);
42}
ms = new MemoryStream ();
The IMG. Save (MS, Imageformat.gif);
The context. Response.clearcontent ();
The context. Response.ContentType = "Image/gif";
The context. Response.BinaryWrite (Ms. ToArray ());
The context. Response.End ();
49}
Catch
51 {}
Finally
53 {
if (g!= null)
G.dispose ();
The IMG. Dispose ();
57}
58}
59}
All benefits will be harmful, the biggest drawback is to increase the system overhead, the client every request to filter again, the performance of nature to discount. Do not know which friends have a better way, or optimize the method, together to explore.



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.