使用HttpHandler隐藏图片真实地址,轻松实现防盗链

正着手准备做个图片共享网站,考虑到图片的防盗链,剥离出了BlogEngine的ImageHandler,并进行简单地修改。用来做隐藏图片的真实地址和防盗链。

原文地址:http://blog.moozi.net/archives/2008/10/12/use-httphandler-in-csharp-to-hide-the-true-address-of-the-picture/ 

 

     public   class  ImageHandler : IHttpHandler
ExpandedBlockStart.gifContractedBlock.gif    
{
        
public bool IsReusable
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return false; }
        }

        
public void ProcessRequest(HttpContext context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (!string.IsNullOrEmpty(context.Request.QueryString["picture"]))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
string fileName = context.Request.QueryString["picture"];
                OnServing(fileName);
                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
string folder = "App_Data/Picture/";
                    FileInfo fi 
= new FileInfo(context.Server.MapPath(folder) + fileName);

                    
if (fi.Exists &&
                        fi.Directory.FullName.ToUpperInvariant().Contains(
                            Path.DirectorySeparatorChar 
+ "PICTURE"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
//context.Response.Cache.SetCacheability(HttpCacheability.Public);
                        
//context.Response.Cache.SetExpires(DateTime.Now.AddYears(1));

                        
//if (Utils.SetConditionalGetHeaders(fi.CreationTimeUtc))
                        
//    return;

                        
int index = fileName.LastIndexOf("."+ 1;
                        
string extension = fileName.Substring(index).ToUpperInvariant();

                        
// Fix for IE not handling jpg image types
                        if (string.Compare(extension, "JPG"== 0)
                            context.Response.ContentType 
= "image/jpeg";
                        
else
                            context.Response.ContentType 
= "image/" + extension;

                        context.Response.TransmitFile(fi.FullName);
                        OnServed(fileName);
                    }

                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        OnBadRequest(fileName);
                        context.Response.ContentType 
= "image/gif";
                        context.Response.TransmitFile(context.Server.MapPath(folder) 
+ "nophoto.gif");
                        
//context.Response.Redirect(Utils.AbsoluteWebRoot + "error404.aspx");
                    }

                }

                
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    OnBadRequest(ex.Message);
                    
//context.Response.Redirect(Utils.AbsoluteWebRoot + "error404.aspx");
                }

            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Occurs before the requested image is served.
        
/// </summary>

        public static event EventHandler<EventArgs> Serving;
        
private static void OnServing(string file)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (Serving != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Serving(file, EventArgs.Empty);
            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Occurs when a file is served.
        
/// </summary>

        public static event EventHandler<EventArgs> Served;
        
private static void OnServed(string file)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (Served != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Served(file, EventArgs.Empty);
            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Occurs when the requested file does not exist.
        
/// </summary>

        public static event EventHandler<EventArgs> BadRequest;
        
private static void OnBadRequest(string file)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (BadRequest != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                BadRequest(file, EventArgs.Empty);
            }

        }

    }

备注:不太明白这里的private static void OnServing(string file)private static void OnServed(string file)private static void OnBadRequest(string file)这些方法以及事件的作用,还请网友们不吝赐教。

如果要实现防盗链,只需要加入对Request.UrlReferrer.Host的判断就OK了。

在Web.config中加入:

       < httpHandlers >
        
< add verb = " * "  path = " image.axd "  type = " MzMobile.Web.HttpHandlers.ImageHandler, MzMobile.Web "  validate = " false " />
      
</ httpHandlers >

这样,在App_Data文件夹中创建"Picture"目录,并在目录下存放图片文件,这样就可以用image.axd?picture=PictureName (PictureName指你的图片名称)来访问图片了。

BlogEngine有太多值得学习的地方了,好东西啊!

转载于:https://www.cnblogs.com/moozi/archive/2008/10/12/1308960.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值