当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  图片、文件防盗链程序

 图片、文件防盗链程序

点击次数:23 次 发布日期:2008-11-26 11:11:01 作者:源代码网
源代码网推荐      关于图片盗链这个问题,毕竟是自己的劳动成功,很多人不希望别人就那么轻易地偷走了。 这个功能在很多的论坛上都具有,可能是因为盗链的行为太多了吧
源代码网推荐   反盗链的程序其实很简单,熟悉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}
源代码网推荐   凡是有利必有害,这样做最大的缺点就是增加了系统开销,客户端的每一请求都要过滤一遍,性能自然要打折扣了。不知道哪位朋友有更好的办法,或者优化的方法,一起来探讨探讨。
源代码网推荐  http://www.cnblogs.com/nowind/archive/2007/01/16/622016.html
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华