Asp.net mvc 运行时的动态Ip限制
我想向MVC5项目添加/删除运行时的IP限制 我搜索了一下,找到了两种方法Asp.net mvc 运行时的动态Ip限制,asp.net-mvc,iis,dynamic-ip,Asp.net Mvc,Iis,Dynamic Ip,我想向MVC5项目添加/删除运行时的IP限制 我搜索了一下,找到了两种方法 在运行时更改动态Ip限制模块 using System; using System.Text; using Microsoft.Web.Administration; internal static class Sample { private static void Main() { using (ServerManager serverManager = ne
using System;
using System.Text;
using Microsoft.Web.Administration;
internal static class Sample
{
private static void Main()
{
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity", "Default Web Site");
ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection();
ConfigurationElement addElement = ipSecurityCollection.CreateElement("add");
addElement["ipAddress"] = @"192.168.100.1";
addElement["allowed"] = false;
ipSecurityCollection.Add(addElement);
ConfigurationElement addElement1 = ipSecurityCollection.CreateElement("add");
addElement1["ipAddress"] = @"169.254.0.0";
addElement1["subnetMask"] = @"255.255.0.0";
addElement1["allowed"] = false;
ipSecurityCollection.Add(addElement1);
serverManager.CommitChanges();
}
}
}
serverManager.CommitChanges
是否重新启动IIS或应用程序
为此,我将使用节流
如果应用程序或IIS尚未重新启动,我更喜欢第一种方式,因为它位于IIS级别
您对哪种方法最好或其他方法有什么建议吗?第一种方法会重新启动应用程序。第二种方法是操作级别(对象已经创建) 因此,我在Begin_请求时阻止/重定向请求。我正在将要阻止的IP添加到缓存中。然后我在开始请求时读取缓存值,若请求ip在黑名单中,我将其重定向到404.html
private void Application_BeginRequest(object sender, EventArgs e)
{
using (var mylifeTimeScope = IoCBootstrap.Container.BeginLifetimeScope())
{
var ipHelper = mylifeTimeScope.Resolve<IIpHelper>();
if (ipHelper.BlackListIp())
{
HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.Redirect("404.html");
}
}
}
private void应用程序\u BeginRequest(对象发送方,事件参数e)
{
使用(var mylifeTimeScope=IoCBootstrap.Container.BeginLifetimeScope())
{
var ipHelper=mylifeTimeScope.Resolve();
if(ipHelper.BlackListIp())
{
HttpContext.Current.Response.StatusCode=404;
HttpContext.Current.Response.Redirect(“404.html”);
}
}
}
ipHelper.BlackListIp()
检查ip是否在黑名单中