首页 打工社区 个人空间(博客) 天易免费影院 通行证】【登陆】【注册
天易网
 打工动态 | 打工路上 | 社会关注 | 工薪一族 | 求职指南 | 薪资行情 | 创业故事 | 成功人生 | 政策法规
 情感方舟 | 笑话娱乐 | 都市时尚 | 明星八卦 | 小说连载 | 地方风情 | 打工文学 | 技术长廊 | 骗术大观
 您的位置:首页 > 技术长廊 > WEB开发 > ASP.NET > 正文
图片地址防盗链,通过IHttpHandler实现 2
http://www.130cn.com  2007-5-19 0:00:00   评论0 字体:[ ] [收藏] [投稿]

     /// <summary>
   /// 输出真实文件
   /// </summary>
   /// <param name="response"></param>
   /// <param name="context"></param>
   private void Real(HttpResponse response,HttpRequest request)
   {
   FileInfo file = new System.IO.FileInfo(request.PhysicalPath);
  
   response.Clear();
  
   response.AddHeader("Content-Disposition", "filename=" + file.Name);
  
   response.AddHeader("Content-Length", file.Length.ToString());
  
   string fileExtension = file.Extension.ToLower();
  
  
   //这里选择输出的文件格式
   //可以参考http://ewebapp.cnblogs.com/articles/234756.HTML增加对更多文件格式的支持.
  
  
   switch (fileExtension)
   {
  
   case "mp3":
   response.ContentType = "audio/mpeg3";
   break;
  
   case "mpeg":
  
   response.ContentType = "video/mpeg";
   break;
  
   case "jpg":
  
   response.ContentType = "image/jpeg";
   break;
  
   case "bmp":
  
   response.ContentType = "image/bmp";
   break;
  
   case "gif":
  
   response.ContentType = "image/gif";
   break;
  
   case "doc":
  
   response.ContentType = "application/msword";
  
   break;
   case "CSS":
  
   response.ContentType = "text/CSS";
   break;
  
   default:
  
   response.ContentType = "application/octet-stream";
   break;
  
   }
  
  
   response.WriteFile(file.FullName);
  
   response.End();
   }
  
  
   /// <summary>
   /// 确认字符串是否为空
   /// </summary>
   /// <param name="_value"></param>
   /// <returns></returns>
   private bool Empty(string _value)
   {
   if(_value == null | _value == string.Empty | _value == "")
   {
   return true;
   }
   else
   {
   return false;
   }
   }
  
  
   /// <summary>
   /// 检查是否是非法链接
   /// </summary>
   /// <param name="context"></param>
   /// <param name="_myDomain"></param>
   /// <returns></returns>
   private bool errorLink(HttpContext context,out string _myDomain)
   {
   HttpResponse response = context.Response;
   string myDomain = context.Request.ServerVariables["SERVER_NAME"];
   _myDomain = myDomain ;
   string myDomainIp = context.Request.UserHostAddress;
  
  
   eWebapp_NoLink = ConfigurationSettings.AppSettings["eWebapp_NoLink"];
   eWebapp_AllowLink = ConfigurationSettings.AppSettings["eWebapp_AllowLink"];
  
   try
   {
   eWebapp_AllowOnlyFile = Convert.ToBoolean(ConfigurationSettings.AppSettings["eWebapp_AllowOnlyFile"]);
   }
   catch
   {
   eWebapp_AllowOnlyFile = true;
   }
  
  
   if(context.Request.UrlReferrer != null)
   {
  
  
   //判定referDomain是否存在网站的IP或域名
   string referDomain = context.Request.UrlReferrer.AbsoluteUri.Replace(context.Request.UrlReferrer.AbsolutePath,"");
   string myPath = context.Request.RawUrl;
  
   if(referDomain.IndexOf(myDomainIp) >=0 | referDomain.IndexOf(myDomain)>=0)
   {
   return false;
   }
   else
   {
   //这里使用正则表达对规则进行匹配
   try
   {
   Regex myRegex ;
  
   //检查允许匹配
   if(!Empty(eWebapp_AllowLink))
   {
  
   myRegex = new Regex(eWebapp_AllowLink);
  
   if(myRegex.IsMatch(myPath))
   {
   return false;
   }
  
   }
  
  
   //检查禁止匹配
   if(!Empty(eWebapp_NoLink))
   {
  
   myRegex = new Regex(eWebapp_NoLink);
   if(myRegex.IsMatch(myPath))
   {
   return true;
   }
   else
   {
   return false;
   }
  
   }
  
   return true;
  
   }
   catch
   {
   //如果匹配出错,链接错误
   return true;
   }
   }
   }
   else
   {
   //是否允许直接访问文件
   if(eWebapp_AllowOnlyFile)
   {
   return false;
   }
   else
   {
   return true;
   }
   }
  
   }
  
   }
  
  }
  http://www.cnblogs.com/zhouxujian/archive/2006/10/12/527673.HTML
    


 
 【查看评论】【收藏此页】【打印】【关闭】【讨论区
 
 天易通行证: 密码: 注册新用户 所有评论0
 相关信息
 
内容搜索
选择频道
推荐信息
 
热门信息
 

声明:1、本站所有文章除特殊注明“本站原创”外,其它版权归作者所有!
   2、任何转载本站原创文章必须注明来自天易网,否则将追究其法律责任!
   3、本站部分文章来自网上,如对您造成侵权等行为请联系我们,我们将及时删除!

 
 
关于我们 | 意见反馈 | 在线投稿 | 广告服务 | 网站地图 | 设为首页 | 加入收藏 | 友情链接
Copyright © 2005 - 2015 130CN.COM Inc. All Rights Reserved  天易网 - 版权所有
客户服务:Service@130cn.com 技术支持:Support@130cn.com 在线QQ:17678824,32812224 粤ICP备06089049号
网络警察