Regarding the issue of hotlinking of pictures, after all, it is their own work that succeeds, and many people do not want others to steal it so easily. This function is available on many forums, maybe because there are too many hotlinking behaviors.
The anti-leeching program is actually very simple. If you are familiar with the ASP.NET application life cycle, you can easily write one. Just use HttpModule to intercept the request in the BeginRequest event. The remaining work is to filter, and then filter again!
If you are not familiar with HttpModule, you can check it out on MSDN. The introduction is very detailed. The address is: ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_aspnetcon/html/f1d2910f- 61d0-4541-8af8-c3c108ca351f.htm. No more nonsense here
1private void Application_BeginRequest(Object source, EventArgs e)
2{
3 HttpApplication application = (HttpApplication)source;
4 HttpContext context = application.Context;
5 bool isSafe = true; //Is the link legal?
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 whether the file suffix is within the list of excluded file types
11 bool isHas = ClassLibrary.RData.RString.StrIsIncUseSC(exp, config.ImgSafeType.Split('|'));
12 if (isHas)
13 {
14 string domainOutter = context.Request.UrlReferrer.Authority.ToLower(); //Contains domain name and port
15 ArrayList arry = Common.Cache.GetDomainValid();//Get the legal domain name binding list defined by the system
16 isSafe = arry.Contains(domainOutter); //Determine whether the currently requested domain name is within the legal list
17}
18}
19 //The following is the output when it is illegal. If there is a default replacement image, it will be output. If not, one will be generated in the format of .gif.
20 if (!isSafe)
twenty one {
22 Bitmap img = null;
23 Graphics g = null;
24 MemoryStream ms = null;
25
26 try
27 {
28 string picPath = ClassLibrary.RPath.GetFullDirectory("images/unlawful.gif");
29 if (File.Exists(picPath))
30 {
31 img = new Bitmap(picPath, false);
32}
33 else
34 {
35 img = new Bitmap(64, 64);
36 g = Graphics.FromImage(img);
37 g.Clear(Color.White);
38 Font f = new Font("宋体,黑体,Arial", 9,FontStyle.Bold);
39 SolidBrush s = new SolidBrush(Color.Red);
40 g.DrawString(Resources.Message.LawlessLink, f, s, 1, 20);
41 img.Save(picPath, ImageFormat.Gif);
42 }
43 ms = new MemoryStream();
44 img.Save(ms, ImageFormat.Gif);
45 context.Response.ClearContent();
46 context.Response.ContentType = "image/Gif";
47 context.Response.BinaryWrite(ms.ToArray());
48 context.Response.End();
49 }
50 catch
51 { }
52 finally
53 {
54 if(g != null )
55 g.Dispose();
56 img.Dispose();
57 }
58 }
59}
Everything that is beneficial must be harmful. The biggest disadvantage of this is that it increases system overhead. Every request from the client must be filtered, and performance will naturally be compromised. I don’t know if any friend has a better way or an optimization method, let’s discuss it together.
http://www.cnblogs.com/nowind/archive/2007/01/16/622016.html