C#进阶-基于.NET Framework 4.x框架实现ASP.NET WebForms项目IP拦截器

ops/2024/9/23 18:00:25/
http://www.w3.org/2000/svg" style="display: none;">

在这篇文章中,我们将探讨如何在 ASP.NET WebForms 中实现IP拦截器,以便在 ASMX Web 服务方法HTTP 请求 中根据IP地址进行访问控制。我们将使用自定义的 SoapExtensionIHttpModule 来实现这一功能,并根据常用的两种文本传输协议:SOAP协议HTTP协议进行分别讲解。


一、创建ASMX接口文件

首先,我们创建一个 ASP.NET WebForms 项目,创建 TestAsmxProject.Asmx 文件,并定义里面的 WebService 服务。
如果不会创建 ASMX 文件,可以参考我的上一篇文章:C#进阶-ASP.NET WebForms调用ASMX的WebService接口。

TestAsmxProject.Asmx 代码如下:

using System.Web.Services;namespace TestAsmxProject.Asmx
{/// <summary>/// Test 的摘要说明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.ComponentModel.ToolboxItem(false)]// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 [System.Web.Script.Services.ScriptService]public class Test : System.Web.Services.WebService{[WebMethod]public string HelloWorld(){return "Hello World";}[WebMethod(Description = "计算两个数的和")]public int Add(int a, int b){return a + b;}}
}

从这个类我们可以看到,目前是有两个 WebService 方法:HelloWorld 方法和 Add方法。调用 HelloWorld 方法会返回 "Hello World" 字符串;用 Add 方法,需要传入 ab 两个参数,会返回 ab 相加的和。


二、基于SOAP协议的拦截器实现

创建一个自定义的 SoapExtension 来实现IP拦截。我们还需要一个自定义属性来指定允许的IP地址。

1. 创建IpFilterAttribute类

新建 Filter 文件夹,再在 Filter 文件夹里新建 SOAP 文件夹。

https://i-blog.csdnimg.cn/direct/e6955a2e6f214a1b94d10aed08d00e41.png" alt="请添加图片描述" />

SOAP 文件夹里创建 IpFilterAttribute 类。

https://i-blog.csdnimg.cn/direct/036dfe0540444c0fa3d3d74791cd4df5.png" alt="在这里插入图片描述" />

https://i-blog.csdnimg.cn/direct/b503bef2b53a47fb869386ae98b3b0f9.png" alt="在这里插入图片描述" />

IpFilterAttribute.cs 代码如下:

using System;
using System.Web.Services.Protocols;namespace TestAsmxProject.Filter.SOAP
{[AttributeUsage(AttributeTargets.Method)]public class IpFilterAttribute : SoapExtensionAttribute{public override Type ExtensionType => typeof(IpFilter);public override int Priority { get; set; }public string[] AllowedIps { get; private set; }public IpFilterAttribute(params string[] ips){AllowedIps = ips.Length > 0 ? ips : null;Priority = 1;}} 
}

https://i-blog.csdnimg.cn/direct/8b431344738b44c2b70397be1e0203b3.png" alt="在这里插入图片描述" />


2. 创建基于SoapExtension的IpFilter注解类

创建 IpFilter.cs,继承 SoapExtension

https://i-blog.csdnimg.cn/direct/46f477de14f64953a74ba3697f625f7f.png" alt="在这里插入图片描述" />

IpFilter.cs 代码如下:

using System;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Services.Protocols;namespace TestAsmxProject.Filter.SOAP
{public class IpFilter : SoapExtension{private string[] allowedIps;public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute){var ipFilterAttribute = attribute as IpFilterAttribute;return ipFilterAttribute?.AllowedIps;}public override object GetInitializer(Type serviceType){return null;}public override void Initialize(object initializer){allowedIps = initializer as string[];}public override void ProcessMessage(SoapMessage message){switch (message.Stage){case SoapMessageStage.BeforeSerialize:break;case SoapMessageStage.AfterSerialize:break;case SoapMessageStage.BeforeDeserialize:CheckIpValidation(message);break;case SoapMessageStage.AfterDeserialize:break;}}private void CheckIpValidation(SoapMessage message){try{string clientIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];if (string.IsNullOrEmpty(clientIp)){clientIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];}if (!IsValidIp(clientIp)){HttpContext.Current.Response.Clear();HttpContext.Current.Response.StatusCode = 403;HttpContext.Current.Response.ContentType = "text/plain";HttpContext.Current.Response.Write("Access denied: Your IP address is not allowed.");HttpContext.Current.Response.End();}}catch (Exception ex){throw;}}private bool IsValidIp(string ip){string configIps = ConfigurationManager.AppSettings["IpWhiteList"];string[] configAllowedIps = !string.IsNullOrWhiteSpace(configIps)? configIps.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries): new string[] { };var allAllowedIps = (allowedIps == null ? new string[] { } : allowedIps).Concat(configAllowedIps).ToArray();return allAllowedIps.Any(allowIp => ip == allowIp);}}
}

https://i-blog.csdnimg.cn/direct/7a5c041285f54a7998646dfb87cc7660.png" alt="在这里插入图片描述" />


3. 配置web.config中IP白名单

web.config 文件中配置白名单IP列表,在 IpFilter.cs 里我们已经写过该逻辑,web.config 文件中白名单内的 IpWhiteList 里的IP是全局白名单,无论 WebService服务方法 上是否有 [IpFilter] 注解。

<configuration><appSettings><add key="IpWhiteList" value="127.0.0.1,192.168.1.1" /></appSettings>
</configuration>

4. 在WebService方法上添加注解

WebService服务方法 上使用 [IpFilter] 注解,可以定义该方法的专属IP白名单(包含 web.config 中的全局白名单),如果不设定,则仅使用 web.config 中的白名单。

加了 [IpFilter] 注解后的 TestAsmxProject.Asmx 代码如下:

using System.Web.Services;
using TestAsmxProject.Filter.SOAP;namespace TestAsmxProject.Asmx
{/// <summary>/// Test 的摘要说明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.ComponentModel.ToolboxItem(false)]// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 [System.Web.Script.Services.ScriptService]public class Test : System.Web.Services.WebService{[WebMethod][IpFilter] // 不传入指定IP,使用web.config中的白名单public string HelloWorld(){return "Hello World";}[WebMethod(Description = "计算两个数的和")][IpFilter("127.0.0.1", "192.168.1.1")] // 此处为该方法的IP白名单,加上web.config中的白名单共同生效public int Add(int a, int b){return a + b;}}
}

https://i-blog.csdnimg.cn/direct/f0e8a0d36c5849ef8320e9b4886e87c1.png" alt="在这里插入图片描述" />


三、基于HTTP协议的拦截器实现

接下来,我们创建一个基于HTTP协议的拦截器来实现IP拦截。同样,我们需要一个自定义属性来指定允许的IP地址。

1. 创建IpFilterAttribute类

新建 Filter 文件夹,再在 Filter 文件夹里新建 HTTP 文件夹。

https://i-blog.csdnimg.cn/direct/e6955a2e6f214a1b94d10aed08d00e41.png" alt="请添加图片描述" />

SOAP 文件夹里创建 IpFilterAttribute 类。

https://i-blog.csdnimg.cn/direct/892c6f7aef4d45fb8a0cb8f10f67f537.png" alt="在这里插入图片描述" />

https://i-blog.csdnimg.cn/direct/b503bef2b53a47fb869386ae98b3b0f9.png" alt="在这里插入图片描述" />

IpFilterAttribute.cs 代码如下:

using System;
using System.Configuration;namespace TestAsmxProject.Filter.HTTP
{[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]public sealed class IpFilterAttribute : Attribute{public string[] AllowedIps { get; private set; }public IpFilterAttribute(params string[] ips){string configIps = ConfigurationManager.AppSettings["IpWhiteList"];var configAllowedIps = !string.IsNullOrEmpty(configIps)? configIps.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries): new string[] { };AllowedIps = new string[ips.Length + configAllowedIps.Length];ips.CopyTo(AllowedIps, 0);configAllowedIps.CopyTo(AllowedIps, ips.Length);}public bool IsAllowedIp(string userIp){return AllowedIps.Any(ip => userIp == ip);}}
}

2. 创建基于IHttpModule的IpFilter注解类

创建 IpFilter.cs,继承 IHttpModule

https://i-blog.csdnimg.cn/direct/46f477de14f64953a74ba3697f625f7f.png" alt="在这里插入图片描述" />

IpFilter.cs 代码如下:

using System;
using System.Linq;
using System.Reflection;
using System.Web;namespace TestAsmxProject.Filter.HTTP
{public class IpFilter : IHttpModule{public void Init(HttpApplication context){context.PreRequestHandlerExecute += new EventHandler(OnPreRequestHandlerExecute);}private void OnPreRequestHandlerExecute(object sender, EventArgs e){HttpApplication application = (HttpApplication)sender;HttpContext context = application.Context;if (context.Request.Path.Contains(".asmx")){string userIp = context.Request.UserHostAddress;string methodName = context.Request.PathInfo.Replace("/", "");Type webServiceType = GetWebServiceType(context.Request.Path);if (webServiceType != null){MethodInfo methodInfo = webServiceType.GetMethod(methodName);if (methodInfo != null){var attribute = methodInfo.GetCustomAttribute<IpFilterAttribute>();if (attribute != null && !attribute.IsAllowedIp(userIp)){context.Response.StatusCode = 403;context.Response.ContentType = "text/plain";context.Response.Write("Access denied: Your IP address is not allowed.");context.Response.End();}}}}}private Type GetWebServiceType(string path){string serviceName = path.Split('/')[2].Split('.').First();string namespacePrefix = "";string typeName = $"{namespacePrefix}.{serviceName}";Type serviceType = Type.GetType(typeName);if (serviceType == null){var assemblies = AppDomain.CurrentDomain.GetAssemblies();foreach (var assembly in assemblies){serviceType = assembly.GetType(typeName);if (serviceType != null){break;}}}return serviceType;}public void Dispose() { }}
}

3. 配置web.config中白名单和模块

web.config 文件中配置白名单IP列表,在 IpFilter.cs 里我们已经写过该逻辑,web.config 文件中白名单内的 IpWhiteList 里的IP是全局白名单,无论 WebService服务方法 上是否有 [IpFilter] 注解。

<configuration><appSettings><add key="IpWhiteList" value="127.0.0.1,192.168.1.1" /></appSettings><system.web><httpModules><add name="IpFilter" type="TestAsmxProject.Filter.HTTP.IpFilter"/></httpModules></system.web><system.webServer><validation validateIntegratedModeConfiguration="false" /><modules runAllManagedModulesForAllRequests="true"><add name="IpFilter" type="TestAsmxProject.Filter.HTTP.IpFilter"/></modules></system.webServer>
</configuration>

4. 在WebService方法上添加注解

WebService服务方法 上使用 [IpFilter] 注解,可以定义该方法的专属IP白名单(包含 web.config 中的全局白名单),如果不设定,则仅使用 web.config 中的白名单。

加了 [IpFilter] 注解后的 TestAsmxProject.Asmx 代码如下:

using System.Web.Services;
using TestAsmxProject.Filter.HTTP;namespace TestAsmxProject.Asmx
{/// <summary>/// Test 的摘要说明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.ComponentModel.ToolboxItem(false)]// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 [System.Web.Script.Services.ScriptService]public class Test : System.Web.Services.WebService{[WebMethod][IpFilter] // 不传入指定IP,使用web.config中的白名单public string HelloWorld(){return "Hello World";}[WebMethod(Description = "计算两个数的和")][IpFilter("127.0.0.1", "192.168.1.1")] // 此处为该方法的IP白名单,加上web.config中的白名单共同生效public int Add(int a, int b){return a + b;}}
}

https://i-blog.csdnimg.cn/direct/037993cb94274576a6db120d293843f2.png" alt="在这里插入图片描述" />


四、IP拦截器实现总结

通过上述步骤,我们成功实现了在 ASP.NET WebForms 中基于IP地址的访问控制。我们分别使用自定义的 SoapExtensionIHttpModule,实现了对ASMX Web服务方法和HTTP请求的IP拦截

1. 自定义 SoapExtension

自定义的 SoapExtension 通过重载 ProcessMessage 方法,在SOAP消息处理的不同阶段进行IP地址的验证。通过检查请求的IP地址并与允许的IP列表进行比较,我们可以在消息反序列化之前阻止不符合条件的请求,从而有效地控制对Web服务方法的访问。这种方法特别适用于基于SOAP的Web服务,能够在服务方法调用之前进行精细的访问控制。

2. 自定义 IHttpModule

自定义的 IHttpModule 通过实现 Init 方法并注册 PreRequestHandlerExecute 事件,在每个HTTP请求处理之前执行IP地址验证。通过反射获取请求对应的方法,并检查方法上的自定义属性 IpFilterAttribute,我们可以动态地对特定方法应用IP过滤规则。结合 web.config 中配置的白名单IP地址,这种方法能够灵活地扩展和维护IP访问控制规则,适用于一般的HTTP请求拦截需求。

3. 本文提供方法的实现优势

这种IP拦截器的实现方法不仅增强了应用程序的安全性,还具有良好的扩展性和可维护性。开发者可以根据具体需求,通过配置文件或代码注解灵活地管理允许的IP地址。通过将安全控制逻辑封装在自定义模块和扩展中,可以保持业务代码的简洁和可读性。

希望这篇文章对你在ASP.NET WebForms应用中的IP访问控制有所帮助。


http://www.ppmy.cn/ops/87649.html

相关文章

Python前沿技术:机器学习与人工智能

Python前沿技术&#xff1a;机器学习与人工智能 一、引言 随着科技的飞速发展&#xff0c;机器学习和人工智能&#xff08;AI&#xff09;已经成为了计算机科学领域的热门话题。Python作为一门易学易用且功能强大的编程语言&#xff0c;已经成为了这两个领域的首选语言之一。本…

Python版【植物大战僵尸 +源码】

文章目录 粉丝 专属&#xff1a;写在前面&#xff1a;功能实现环境要求怎么玩个性化定义项目演示&#xff1a;源码分享Map地图:Menubar.py主菜单 主函数&#xff1a;项目开源地址 粉丝 专属&#xff1a; 写在前面&#xff1a; 今天给大家推荐一个Gtihub开源项目&#xff1a;Py…

亲测有效 Java.sql.SQLException: Access denied for user ‘root‘@‘localhost‘ (using password: YES)解决方法

文章目录 问题分析报错原因解决思路解决方法1. 确认密码2. 检查用户权限3. 重置密码4. 修复用户表5. 在Java代码中使用正确的凭据 java.sql.SQLException: Access denied for user rootlocalhost (using password: YES) 这个异常通常表示在尝试使用 root 用户和提供的密码从本…

Conda虚拟环境下libp11-kit.so.0: undefined symbol: ffi_type_pointer...问题解决

Conda虚拟环境下libp11-kit.so.0: undefined symbol: ffi_type_pointer...问题解决 1 背景说明2 报错原因3 解决方法4 补充说明 1 背景说明 最近需要在Conda虚拟环境下运行ROS相关代码&#xff0c;其中在运行代码import moveit_commander时&#xff0c;返回报错ImportError: /…

【C++】stack和queue的模拟实现 双端队列deque的介绍

&#x1f525;个人主页&#xff1a; Forcible Bug Maker &#x1f525;专栏&#xff1a; STL || C 目录 &#x1f308;前言&#x1f525;stack的模拟实现&#x1f525;queue的模拟实现&#x1f525;deque&#xff08;双端队列&#xff09;deque的缺陷 &#x1f308;为什么选择…

【AI 大模型】OpenAI 接口调用 ① ( 安装 openai 软件包 | 查看 openai 软件包版本 | PyCharm 中开发 Python 程序调用 OpenAI 接口 )

文章目录 一、安装 Python SDK二、安装 OpenAI Python SDK1、安装 openai 软件包2、查看 openai 软件包版本3、openai 接口参考文档 三、PyCharm 中开发 Python 程序调用 OpenAI 接口1、PyCharm 创建 Python 项目2、API Key 配置用法3、GPT3 模型和 GPT4 模型4、Python 代码示例…

java常见面试题(160道)

1. JDK 和 JRE 有什么区别&#xff1f; JDK&#xff1a;Java Development Kit 的简称&#xff0c;Java 开发工具包&#xff0c;提供了 Java 的开发环境和运行环境。JRE&#xff1a;Java Runtime Environment 的简称&#xff0c;Java 运行环境&#xff0c;为 Java 的运行提供了…

【Java】还不懂this关键字?一分钟彻底弄懂this关键字

博主简介&#xff1a;努力学习的预备程序媛一枚~博主主页&#xff1a; 是瑶瑶子啦所属专栏: Java岛冒险记【从小白到大佬之路】 前言 问题&#xff1a;为什么会存在this? 在上一篇【JavaSE】一文看懂构造器/构造方法&#xff08;Cunstructor&#xff09;中&#xff0c;我们已…