English
 电子信箱
 加入收藏

  威盾防火墙 >> 新闻中心 >> 威盾新闻 >> 图片防盗链

 

图片防盗链

威盾防火墙 2015-01-15

 

using System; 

using System.Web; 

 

 

/// <summary> 

/// ImageProtect 的摘要说明 

/// </summary> 

public class ImageProtect : IHttpHandler 

    public ImageProtect() 

    { 

        // 

        // TODO: 在此处添加构造函数逻辑 

        // 

    } 

    public void ProcessRequest(HttpContext context) 

    { 

        //判断是否是本地引用,如果是则返回给客户端正确的图片, 

        //假设我们的网站的地址为http://localhost:6999,演示时可根据需要修改        

        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; 

        } 

    } 

 

/*

* 在配置文件中

*<httpHandlers>

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

</httpHandlers>

*/   

摘自 幸福的猪的专栏


相关内容: 最新内容:
D12-Nginx-利用Referer防盗链[2015-01-15]
解析asp.net防盗链技术[2015-01-15]
初识防盗链技术[2015-01-15]
Apache 防盗链[2015-01-14]
图片防盗链[2015-01-14]
部分网站允许空白referer的防盗链图片的js破解代码[2015-01-14]
D12-Nginx-利用Referer防盗链[2015-01-15]
解析asp.net防盗链技术[2015-01-15]
初识防盗链技术[2015-01-15]
Apache 防盗链[2015-01-14]
图片防盗链[2015-01-14]
部分网站允许空白referer的防盗链图片的js破解代码[2015-01-14]