All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Asp.net数据库操作类
Asp.net与SQL一起打包部署安装_[Asp.Net教程]
Ajax标签导航实例详解-Javascript技巧篇_[AJAX教程]
XML的严格格式_[XML教程]
编写XHTML需要注意的问题以及和HTML的区别_[Html教程]
visual c++中的Document与View概述
浅谈SQL,Server数据类型最大值_[SQL,Server教程]
C#网络应用编程基础练习题与答案(八)_.net资料_编程技术
delphi修改数据表中的信息
菜鸟到高手:SQL开发进阶常用精妙语句_[SQL,Server教程]
Reading number is top 10 pictures
The real super beauty7
看看什么叫美景
Li Zongrui hunting video screenshots2
Female model behind the bitterness, often being overcharged4
30 beautiful school beauty6
移民小国也实惠1
不知名的美女素颜照1
Abdominal hit by iron--HangZhou best driver parking save passengers
9.3阅兵全景图7-指挥系统和后勤保障系统梯队
The Soviet union swimsuit exposure in the 70 year1
Download software ranking
C#编程思想
Eclipse-CALMSANNY (second edition)
Tram sex maniac 2 (H) rar bag10
Professional killers2 data package
Tram sex maniac 2 (H) rar bag7
Boxer's Top ten classic battle5
apache-tomcat-6.0.33
I for your crazy
Ashlynn Video3
C语言教程TXT
aaa published in(发表于) 2013/12/15 8:52:46 Edit(编辑)
实例代码:图片、文件防盗链程序_.net资料_编程技术

实例代码:图片、文件防盗链程序_.net资料_编程技术

实例代码:图片、文件防盗链程序_.net资料_编程技术-你的首页-uuhomepage.com

  关于图片盗链这个问题,毕竟是自己的劳动成功,很多人不希望别人就那么轻易地偷走了。 这个功能在很多的论坛上都具有,可能是因为盗链的行为太多了吧


  反盗链的程序其实很简单,熟悉ASP.NET 应用程序生命周期的话很容易就可以写一个,运用HttpModule在BeginRequest事件中拦截请求就ok了,剩下的工作就是过滤,再过滤!


  如果不熟悉HttpModule的话,可以去MSDN上查阅,介绍非常详细,地址:ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_aspnetcon/html/f1d2910f-61d0-4541-8af8-c3c108ca351f.htm。这里就不废话了


1private void Application_BeginRequest(Object source, EventArgs e)
2{
3 HttpApplication application = (HttpApplication)source;
4 HttpContext context = application.Context;
5 bool isSafe = true; //是否合法链接
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 //这里是判断文件后缀名是否在排除的文件类型列表之内
11 bool isHas = ClassLibrary.RData.RString.StrIsIncUseSC(exp, config.ImgSafeType.Split('|'));
12 if (isHas)
13 {
14 string domainOutter = context.Request.UrlReferrer.Authority.ToLower(); //包含域名和端口
15 ArrayList arry = Common.Cache.GetDomainValid();//取系统定义的合法的域名绑定列表
16 isSafe = arry.Contains(domainOutter); //判断当前请求的域名是否在合法列表之内
17 }
18 }
19 //下面就是不合法的时候的输出了,如果有默认替代图片则输出,如果没有就生成一个,格式为.gif
20 if (!isSafe)
21 {
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}


  凡是有利必有害,这样做最大的缺点就是增加了系统开销,客户端的每一请求都要过滤一遍,性能自然要打折扣了。不知道哪位朋友有更好的办法,或者优化的方法,一起来探讨探讨。





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.