当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  图片地址防盗链,通过IHttpHandler实现 1

 图片地址防盗链,通过IHttpHandler实现 1

点击次数:13 次 发布日期:2008-11-26 11:10:59 作者:源代码网
源代码网推荐      /*
源代码网推荐   *
源代码网推荐   * 防盗链IHttpHandler
源代码网推荐   *
源代码网推荐   *
源代码网推荐   * 增加了对文件关键字的选择(即仅对文件名存在某些关键字或不存在某些关键字进行过滤)
源代码网推荐   * 设置web.config中<appSettings>节以下值
源代码网推荐   * string eWebapp_NoLink 如果文件名符合该正确表态式将进行过滤(不设置对所有进行过滤)
源代码网推荐   * string eWebapp_AllowLink 如果文件名符合该正确表态式将不进行过滤(优先权高于AllowLink,不设置则服从AllowLink)
源代码网推荐   * bool eWebapp_ AllowOnlyFile 如果为False,(默认true)则不允许用户直接对该文件进行访问建议为true
源代码网推荐   *
源代码网推荐   *
源代码网推荐   * :)以下设置均可省略,设置只是为了增加灵活性与体验
源代码网推荐   * eWebapp_NoLink_Message 错误信息提示:默认为Link From:域名
源代码网推荐   * eWebapp_Error_Width 错误信息提示图片宽
源代码网推荐   * eWebapp_Error_Height 错误信息提示图片高
源代码网推荐   *
源代码网推荐   *
源代码网推荐   *
源代码网推荐   *
源代码网推荐   * http://ewebapp.net
源代码网推荐   */
源代码网推荐  
源代码网推荐  
源代码网推荐  using System;
源代码网推荐  using System.Web;
源代码网推荐  using System.Drawing;
源代码网推荐  using System.Drawing.Imaging;
源代码网推荐  using System.IO;
源代码网推荐  using System.Configuration;
源代码网推荐  using System.Text.RegularExpressions;
源代码网推荐  
源代码网推荐  namespace eWebapp
源代码网推荐  {
源代码网推荐   /// <summary>
源代码网推荐   /// 防盗链IHttpHandler
源代码网推荐   /// 参考http://www.softat.org/archiver/tid-52114.html
源代码网推荐   ///
源代码网推荐   /// </summary>
源代码网推荐   public class NoLink : IHttpHandler
源代码网推荐   {
源代码网推荐   private string eWebapp_NoLink = string.Empty;
源代码网推荐   private string eWebapp_AllowLink = string.Empty;
源代码网推荐   private bool eWebapp_AllowOnlyFile = true;
源代码网推荐  
源代码网推荐   private string eWebapp_NoLink_Message = string.Empty;
源代码网推荐   private bool error = false;
源代码网推荐  
源代码网推荐   public NoLink()
源代码网推荐   {
源代码网推荐   //
源代码网推荐   // TODO: 在此处添加构造函数逻辑
源代码网推荐   //
源代码网推荐   }
源代码网推荐  
源代码网推荐   public void ProcessRequest(HttpContext context)
源代码网推荐   {
源代码网推荐   eWebapp_NoLink_Message = ConfigurationSettings.AppSettings["eWebapp_NoLink_Message"];
源代码网推荐  
源代码网推荐  
源代码网推荐   string myDomain = string.Empty;
源代码网推荐  
源代码网推荐   error = errorLink(context,out myDomain);
源代码网推荐  
源代码网推荐   if(Empty(eWebapp_NoLink_Message))
源代码网推荐   {
源代码网推荐   eWebapp_NoLink_Message = "Link from :" + myDomain;
源代码网推荐   }
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐   if(error)
源代码网推荐   {
源代码网推荐   //Jpg(context.Response,eWebapp_NoLink_Message);
源代码网推荐   Jpg(context.Response,eWebapp_NoLink_Message);
源代码网推荐   }
源代码网推荐   else
源代码网推荐   {
源代码网推荐   Real(context.Response,context.Request);
源代码网推荐   }
源代码网推荐  
源代码网推荐   }
源代码网推荐  
源代码网推荐   public bool IsReusable
源代码网推荐   {
源代码网推荐   get
源代码网推荐  
源代码网推荐   {
源代码网推荐   return true;
源代码网推荐   }
源代码网推荐   }
源代码网推荐  
源代码网推荐  
源代码网推荐   /// <summary>
源代码网推荐   /// 输出错误信息
源代码网推荐   /// </summary>
源代码网推荐   /// <param name="Response"></param>
源代码网推荐   /// <param name="_word"></param>
源代码网推荐   private void Jpg(HttpResponse Response,string _word)
源代码网推荐   {
源代码网推荐  
源代码网推荐  
源代码网推荐   int myErrorWidth = _word.Length*15;
源代码网推荐   int myErrorHeight = 16;
源代码网推荐   try
源代码网推荐   {
源代码网推荐   int _myErrorWidth = Convert.ToInt32(ConfigurationSettings.AppSettings["eWebapp_Error_Width"]);
源代码网推荐   if(_myErrorWidth > 0 )
源代码网推荐   {
源代码网推荐   myErrorWidth = _myErrorWidth;
源代码网推荐   }
源代码网推荐  
源代码网推荐   }
源代码网推荐   catch
源代码网推荐   {
源代码网推荐  
源代码网推荐   }
源代码网推荐   try
源代码网推荐   {
源代码网推荐   int _myErrorHeight = Convert.ToInt32(ConfigurationSettings.AppSettings["eWebapp_Error_Height"]);
源代码网推荐   if(_myErrorHeight > 0 )
源代码网推荐   {
源代码网推荐   myErrorHeight = _myErrorHeight;
源代码网推荐   }
源代码网推荐   }
源代码网推荐   catch
源代码网推荐   {
源代码网推荐  
源代码网推荐   }
源代码网推荐   Bitmap Img=null;
源代码网推荐   Graphics g=null;
源代码网推荐   MemoryStream ms=null;
源代码网推荐   Img=new Bitmap(myErrorWidth,myErrorHeight);
源代码网推荐   g=Graphics.FromImage(Img);
源代码网推荐   g.Clear(Color.White);
源代码网推荐   Font f=new Font("Arial",9);
源代码网推荐   SolidBrush s=new SolidBrush(Color.Red);
源代码网推荐   g.DrawString(_word,f,s,3,3);
源代码网推荐   ms=new MemoryStream();
源代码网推荐   Img.Save(ms,ImageFormat.Jpeg);
源代码网推荐   Response.ClearContent();
源代码网推荐   Response.ContentType="image/Gif";
源代码网推荐   Response.BinaryWrite(ms.ToArray());
源代码网推荐   g.Dispose();
源代码网推荐   Img.Dispose();
源代码网推荐   Response.End();
源代码网推荐   }
源代码网推荐  
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


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