zoukankan      html  css  js  c++  java
  • Asp.net网站使用HttpHandler实现图片防盗链功能

    第一步:创建PicHandler.cs文件,代码如下:

    using System.Web;

    namespace MyHttpModule
    {
        public class PicHandler:IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                string strFile = context.Server.MapPath(context.Request.FilePath);
                if(context.Request.UrlReferrer==null)
                {
                    context.Response.ContentType = "image/JPEG";
                    context.Response.WriteFile("/error.jpg");
                }
                else
                {
                    if(context.Request.UrlReferrer.Host.IndexOf("youdomain.com")>-1)
                    {
                        context.Response.ContentType = "imae/jpeg";
                        context.Response.WriteFile(strFile);
                    }
                    else
                    {
                        context.Response.ContentType = "image/JPEG";
                        context.Response.WriteFile("/error.jpg");
                    }
                }
            }

            /// <summary>
            
    /// 获取一个值,该值指示其他请求是否可以使用IHttpHander实例。也就是后续的Http请求是不是可以继续使用实现了该接口的类的实例
            
    /// </summary>
            public bool IsReusable
            {
                get { return true; }
            }
        }
    }

    第二步:编译文件成DLL;

     csc /t:library /r:System.Web.dll PicHandler.cs

    第三步:将编译妇的DLL拷贝到Bin目录下;

    第四步:在Web.config中注册这个Handler;

    <httpHandlers>
      <add path="*.jpg" verb="*" type="MyHttpHandler.PicHandler,MyHttpHandler"/>
    </httpHandlers>

    第五步:在IIS中配置应用程序扩展(jpg)

    C:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll

  • 相关阅读:
    bzoj 3438: 小M的作物
    bzoj 4445 [SCOI2015] 小凸想跑步
    hdu 4899 Hero meet devil
    hdu 4898 The Revenge of the Princess’ Knight
    【NOIP1999】拦截导弹
    【OpenJudge】2991:2011 题解
    【cqbzoj】1785:残缺棋盘上放车的方案数 --状压dp --输入毁一生
    【cqbzoj】:1330 Prime DP(Ahio2001 质数和分解)
    【Openjudge:Noi】7891:一元三次方程求解 c++
    【USACO FEB 2010 SILVER】吃巧克力(Chocolate Eating)
  • 原文地址:https://www.cnblogs.com/AngelLee2009/p/2228250.html
Copyright © 2011-2022 走看看