zoukankan      html  css  js  c++  java
  • 全局处理程序图片防盗链

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.SessionState;

    namespace WebApp
    {
        public class Global : System.Web.HttpApplication
        {
            
            //protected void Application_AcquireRequestState(object sender, EventArgs e)
            //{
                
            //}

            protected void Application_BeginRequest(object sender, EventArgs e)
            {
                //判断当前请求,是否请求的images文件夹
                //
                if (Request.RawUrl.ToLower().Contains("/images/"))
                {
                    Response.ContentType = "image/jpeg";

                    //判断域名是否相当
                    Uri referrer = Request.UrlReferrer;
                    Uri url = Request.Url;
                    //盗链请求,输出放盗链图片
                    if (!Compare(url, referrer))
                    {
                        string path = Request.MapPath("~/images/daolian.jpg");
                        Response.WriteFile(path);
                        Response.End();
                    }
                }


                
            }

            bool Compare(Uri u1, Uri u2)
            {
                return Uri.Compare(u1,u2, UriComponents.HostAndPort, UriFormat.SafeUnescaped, StringComparison.CurrentCultureIgnoreCase) == 0;
            }

            protected void Application_AuthenticateRequest(object sender, EventArgs e)
            {

            }


            protected void Application_Start(object sender, EventArgs e)
            {
                
            }

            protected void Session_Start(object sender, EventArgs e)
            {

            }

           

            protected void Application_Error(object sender, EventArgs e)
            {
                //Context.Server.GetLastError();
            }

            protected void Session_End(object sender, EventArgs e)
            {

            }

            protected void Application_End(object sender, EventArgs e)
            {

            }
        }
    }

  • 相关阅读:
    VMware虚拟机中常见的问题汇总
    Windows10下安装VMware虚拟机并搭建CentOS系统环境
    myeclipse2017使用总结
    mybatis如何通过接口查找对应的mapper.xml及方法执行详解
    (转)将SVN从一台服务器迁移到另一台服务器(Windows Server VisualSVN Server)
    (转)Maven中的库(repository)详解 ---repository配置查找构件(如.jar)的远程库
    Git知识讲解
    (转)MyEclipse中使用git
    在SpringBoot中添加Logback日志处理
    (转)Spring Boot干货系列:(七)默认日志logback配置解析
  • 原文地址:https://www.cnblogs.com/eric-gms/p/3474670.html
Copyright © 2011-2022 走看看