图片地址防盗链的IHttpHandler实现方法

分类: asp.net技巧   出处:iocblog整理  更新时间:2009-02-07   添加到收藏  

/*
 * 
 * 防盗链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();
        }

[1] [2] 下一页


Tag: 防盗链



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。