IHttpHandler防止图片链接被盗用

    public class JpegHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string filename = context.Server.MapPath(context.Request.FilePath);
            context.Response.ContentType = "image/jpeg";

            if (context.Request.UrlReferrer == null 
                || context.Request.UrlReferrer.Host == null
                || !context.Request.UrlReferrer.Host.Contains("localhost")
                )
            {
                context.Response.WriteFile("/image/error.jpg");
            }
            else
            {
                context.Request.ContentType = "image/jpeg";
                context.Response.WriteFile(filename);
            }
        }
    }

配置web.config

<add name="Jpeg" verb="*" path="*.jpg"  type="WebApplication2.JpegHandler"/>
原文地址:https://www.cnblogs.com/caoyc/p/6221998.html