第一步 : 实现 IHttpHandler 接口

    

namespace WebHotlinkProtection
{

    public class HotlinkProtectionHandler:IHttpHandler
    {
        public bool IsReusable
        {
            get { throw new NotImplementedException(); }
        }

        public void ProcessRequest(HttpContext context)
        {
            //监听是否本站发起的请求
           if (!context.Request.UrlReferrer.Host.StartsWith("localhost"))
            {
                context.Response.Expires = 0;
                context.Response.Clear();
                context.Response.ContentType = "image/jpg";
                //输出防盗链图片
                context.Response.WriteFile(context.Request.PhysicalApplicationPath + "\\no.jpg");
                context.Response.End();
            }
            else
           { 
                context.Response.Expires = 0;
                context.Response.Clear();
                context.Response.ContentType = "image/jpg";
                context.Response.WriteFile(context.Request.PhysicalPath);
                context.Response.End();
            }
        }
    }
}

第二部:配置web.config

 

      <httpHandlers>
          <add verb="*" path="*.jpg" type="WebHotlinkProtection.HotlinkProtectionHandler,WebHotlinkProtection"/>
      </httpHandlers>

相关文章:

  • 2022-12-23
  • 2021-06-24
  • 2022-12-23
  • 2021-06-09
  • 2021-12-07
  • 2021-07-08
  • 2021-10-04
  • 2022-12-23
猜你喜欢
  • 2021-12-30
  • 2022-01-20
  • 2021-06-14
  • 2022-12-23
  • 2022-02-06
  • 2022-12-23
  • 2021-05-22
相关资源
相似解决方案