• ASP.net 使用HttpHandler实现图片防盗链


    首先是HttpHandler类的代码:

    using System;
    using System.Collections.Generic;
    using System.Web;
    
    namespace HttpHandler
    {
        public class JPEGHandler : IHttpHandler
        {
            #region IHttpHandler 成员
    
         public bool IsReusable
            {
                get { return true; }
            }
    
            public void ProcessRequest(HttpContext context)
            {
                string fileName = 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("localhost") >= 0)
                    {
                        context.Response.ContentType = "image/JPEG";
                        context.Response.WriteFile(fileName);
                    }
                    else
                    {
                        context.Response.ContentType = "image/JPEG";
                        context.Response.WriteFile("/no.jpg");
                    }
                }
            }
    
            #endregion
        }
    }
    
    

    然后是web.config中的代码:

    <configuration>
    	<system.web>
    		<httpHandlers>
    			<add verb="*" path="*.jpg" type="HttpHandler.JPEGHandler"/>
    		</httpHandlers>
    </configuration>
    最后就是在网站根目录下放入no.jpg就可以了。
  • 相关阅读:
    linux 学习笔记 groupadd创建组
    linux学习笔记 4建立用户
    Linux学习笔记 3 权限篇
    Linux学习笔记 1 环境变量 2 vi命令
    指针 以及取地址
    练习题
    weblogic domain creation
    hibernate log4j 输出sql
    练习九 组函数应用
    练习八 spool导出
  • 原文地址:https://www.cnblogs.com/wubin264/p/1774743.html
Copyright © 2020-2023  润新知