asp.net mvc使用IHttpModule拦截所有请求,包括资源文件

发布于:2024-05-07 ⋅ 阅读:(22) ⋅ 点赞:(0)

目录

HttpApplication 类

 添加App_Code文件夹

MyHttpModel2

Web.config添加配置,在iis模块中生效

项目发布后,察看注册的自定义模块


  • 框架集:.NET Framework 4.7
  • web框架:asp.net mvc 5

HttpApplication 类

HttpApplication 类 (System.Web) | Microsoft Learn

https://learn.microsoft.com/zh-cn/dotnet/api/system.web.httpapplication?view=netframework-4.8.1#examples

定义对 ASP.NET 应用程序内所有应用程序对象公用的方法、属性和事件。 此类是用户在 Global.asax 文件中定义的应用程序的基类。

注解
类的 HttpApplication 实例是在 ASP.NET 基础结构中创建的,而不是由用户直接创建。 类的 HttpApplication 一个实例用于在其生存期内处理多个请求。 但是,一次只能处理一个请求。 因此,成员变量可用于存储每个请求的数据。

应用程序引发可由实现 IHttpModule 接口的自定义模块或 Global.asax 文件中定义的事件处理程序代码处理的事件。 实现 接口的 IHttpModule 自定义模块可以放在 App_Code 文件夹中,也可以放在 Bin 文件夹中的 DLL 中。

HttpApplication在 .NET Framework 版本 3.5 中引入。

备注

在集成模式下运行 IIS 7.0 时,App_Code文件夹或 Bin 文件夹中的自定义模块适用于请求管道中的所有请求。 Global.asax 文件中的事件处理程序代码仅适用于映射到 ASP.NET 处理程序的请求。

 添加App_Code文件夹

需要将MyHttpModel2.cs放到App_Code文件夹中,右键web项目,如图操作,添加App_Code文件夹

MyHttpModel2

using RmtSendArticleHandle;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WuZiFenGongSiInfomation.Common;

namespace kantan_shi_yebu.MyHttpModel
{
    /// <summary>
    /// 自定义请求拦截
    /// </summary>
    public class MyHttpModel2 : IHttpModule
    {
        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            //context.AcquireRequestState += Context_AcquireRequestState; ;
            //context.PostAcquireRequestState += Context_PostAcquireRequestState;
            context.BeginRequest += Context_BeginRequest;
            context.Error += Context_Error;
        }

        private void Context_Error(object sender, EventArgs e)
        {
            HttpApplication httpApp = (HttpApplication)sender;
            HttpContext ctx = HttpContext.Current;
            var request = ctx.Request;
            string requestURL = request.Url.OriginalString;
            WuZiFenGongSiInfomation.Common.LogHelpter.AddLog("请求的url=" + requestURL, null, "MyHttpModel2.Context_Error");

            var response = ctx.Response;

            //XSS漏洞拦截,url注入拦截
            //http://localhost:8034/content/images/*~1*/a.aspx?aspxerrorpath=/                 
            if (requestURL.Contains("*") || requestURL.Contains("~"))
            {
                response.StatusCode = 400;
                response.ContentType = "text/plain; charset=utf-8";
                response.Write("http 400,不允许特殊符号");
                ctx.ClearError();
            }
            else if (requestURL.Contains(".aspx"))
            {
                response.StatusCode = 400;
                response.ContentType = "text/plain; charset=utf-8";
                response.Write("http 400,不允许请求.aspx页面");
               
                ctx.ClearError();
            }
          
        }

        private void Context_BeginRequest(object sender, EventArgs e)
        {
            //可以记录所有请求,包括后台请求、静态文件请求url
            HttpApplication httpApp = (HttpApplication)sender;
            HttpContext ctx = HttpContext.Current;
            var request = ctx.Request;
            string requestURL = request.Url.OriginalString;
            WuZiFenGongSiInfomation.Common.LogHelpter.AddLog("请求的url=" + requestURL, null, "MyHttpModel2");
        }

    }
}

Web.config添加配置,在iis模块中生效

<system.webServer>
	<validation validateIntegratedModeConfiguration="false"></validation> 
	<security>
		<requestFiltering>
			<requestLimits maxQueryString="102400" maxAllowedContentLength="102400000" />
		</requestFiltering>
	</security>
 
	<modules>
		<add name="MyHttpModel2" type="kantan_shi_yebu.MyHttpModel.MyHttpModel2"  />
	</modules>
</system.webServer>

项目发布后,察看注册的自定义模块

选中站点,在模块中可以看到,已经注册的模块