图片锁定,防止连接

简介: using System;using System.Web;/// /// ImageProtect 的摘要说明/// public class ImageP...

using  System;
using  System.Web;

///   <summary>
///  ImageProtect 的摘要说明
///   </summary>
public   class  ImageProtect : IHttpHandler
{
    
public  ImageProtect()
    {
        
//
        
//  TODO: 在此处添加构造函数逻辑
        
//
    }
    
public   void  ProcessRequest(HttpContext context)
    {
        
// 判断是否是本地引用,如果是则返回给客户端正确的图片,这里的判断就是用到了前面所述的http请求中所记路的参考页信息
        
// 此处使用了端口,仅用于       
         if  (context.Request.UrlReferrer.Host  ==   " localhost " && context.Request.UrlReferrer.Port == 6999 )               
        {
            
// 设置客户端缓冲中文件过期时间为0,即立即过期。
            context.Response.Expires  =   0 ;                               
            
// 清空服务器端为此会话开辟的输出缓存
            context.Response.Clear();                                   
            
// 获得文件类型
            context.Response.ContentType  =   " image/jpeg " ;                
            
// 将请求文件写入到服务器端为此会话开辟的输出缓存中
            context.Response.WriteFile(context.Request.PhysicalPath);   
            
// 将服务器端为此会话开辟的输出缓存中的信息传送到客户端
            context.Response.End();   
        }
        
else       // 如果不是本地引用,则属于盗链引用,返回给客户端错误的图片
        {
            
// 设置客户端缓冲中文件过期时间为0,即立即过期。
            context.Response.Expires  =   0 ;                               
            
// 清空服务器端为此会话开辟的输出缓存
            context.Response.Clear();                                   
            
// 获得文件类型
            context.Response.ContentType  =   " image/jpeg " ;                
            
// 将特殊的报告错误的图片文件写入到服务器端为此会话开辟的输出缓存中
            context.Response.WriteFile( " ~/images/error.jpg " );                    
            
// 将服务器端为此会话开辟的输出缓存中的信息传送到客户端
            context.Response.End();
        }
    }
    
public   bool  IsReusable
    {
        
get
        {
            
return   false ;
        }
    }
}
 

Web.Config
< httpHandlers >
 
< add verb = " * "  path = " *.jpg "  type = " ImageProtect " />
</ httpHandlers >
 




2 .
#region  IHttpHandler 成员
    
bool  IHttpHandler.IsReusable
    {
        
get  {  return   true ; }
    }
    
void  IHttpHandler.ProcessRequest(HttpContext context)
    {
        
string  FileName  =  context.Server.MapPath(context.Request.FilePath);
        
if  (context.Request.UrlReferrer.Host  ==   null )
        {
            context.Response.ContentType 
=   " image/JPEG " ;
            context.Response.WriteFile(
" /no.jpg " );
        }
        
else
        {
            
if  (context.Request.UrlReferrer.Host.IndexOf( " mydomain.com " >   0 )
            {
                context.Response.ContentType 
=   " image/JPEG " ;
                context.Response.WriteFile(FileName);
            }
            
else
            {
                context.Response.ContentType 
=   " image/JPEG " ;
                context.Response.WriteFile(
" no/jpg " );
            }
        }
    }
    
#endregion

编译成dll。
< httpHandlers >
< add verb = " * "  path = " *.jpg "  type = " JpgHandler, MyDll "   />
</ httpHandlers >
在IIS网站配置中把JPG映射到aspnet_isapi.dll

相关文章
|
关系型数据库 数据库 PostgreSQL
PostgreSQL 数据库实例只读锁定(readonly) - 硬锁定,软锁定,解锁
PostgreSQL 数据库实例只读锁定(readonly) - 硬锁定,软锁定,解锁
2716 0
|
安全
服务器被锁定
服务器被锁定
197 0
缓冲区提前释放,导致H264保存及播放错误
缓冲区提前释放,导致H264保存及播放错误
103 0
|
定位技术
定位锁定方法
定位锁定方法
257 0
定位锁定方法
锁定表格的另一种方法。
function SuoDing() { var rows = document.getElementById("txtRow"); var cols = document.getElementById("txtCol"); Show("DG",rows.value,cols.value); } 锁定的行数:。
734 0
|
SQL 数据库
数据库页已标记为 RestorePending,可能表明磁盘已损坏。要从此状态恢复,请执行还原操作。
错误提示: 消息 829,级别 21,状态 1,第 1 行 数据库 ID 15,页 (1:21826) 已标记为 RestorePending,可能表明磁盘已损坏。要从此状态恢复,请执行还原操作。 引起原因: RestorePending一般是在进行页恢复的过程中出现的,就是在进行了restore操作之后但还没有进行recovery操作之前页的状态。
2552 0