zoukankan      html  css  js  c++  java
  • .net使用httpHandler添加图片防盗链

    .net使用httpHandler添加图片防盗链
    1. 配置web.config:

      <!--图片添加水印的配置-->
                    <httpHandlers>
                            <add verb="*" path="*.jpg" type="LinkHandler" />
                    </httpHandlers>
      <!--图片添加水印的配置结束-->

    2. OutLinkHandler.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Drawing;
    using System.IO;
    
    /// <summary>
    ///HttpHandler 实现防盗链效果
    /// </summary>
    public class LinkHandler:IHttpHandler
    {
        public LinkHandler() { }
                //1. 设置不能允许重用
                public bool IsReusable
                {
                    get { return false; }
                }
                //2. 编写最终处理程序
                public void ProcessRequest(HttpContext context)
                {
                    //context.Request.UrlReferrer.Host   //主机名
                    //context.Request.Url.Port  //端口号
     <wbr>  <wbr>  <wbr>  <wbr>  <wbr>  <wbr>  <wbr>  <wbr> //context.Request.UrlReferrer.Authority   <wbr>//服务器端IP
    
                    //根据Ip地址和端口号判断
                    if (context.Request.UrlReferrer.Authority == "192.168.123.184" &&
                    context.Request.UrlReferrer.Port == context.Request.Url.Port)
                        {
                                context.Response.ContentType="image/jpeg";
                                context.Response.WriteFile(context.Request.PhysicalPath);
                        }
                    else
                        {
                        context.Response.ContentType="image/jpeg";
                        context.Response.WriteFile(context.Request.PhysicalApplicationPath+"images/1/LinkError.jpg");
                        }
                }
    }
  • 相关阅读:
    提高电脑运行效率
    Android_实验小心得_持续补充中......
    LNMP环境搭建wordpress
    php安装
    mysql、MariaDB(yum)
    Nginx配置(yum)
    httpd配置(yum)
    jumpserver环境搭建
    命令
    vsftpd
  • 原文地址:https://www.cnblogs.com/xyyt/p/3978784.html
Copyright © 2011-2022 走看看