【问题标题】:Dynamic Ip Restriction on Runtime运行时动态 IP 限制
【发布时间】:2015-03-31 11:46:29
【问题描述】:

我想为 MVC 5 项目添加/删除运行时的 IP 限制。

我搜索了一下,找到了两种方法。

  1. 在运行时更改动态 IP 限制模块。

    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或应用程序?

  1. Best way to implement request throttling in ASP.NET MVC?

我将为此使用节流。

如果应用程序或 IIS 尚未重新启动,我更喜欢第一种方式,因为它在 IIS 级别。

您有什么建议最好的方法或其他方法吗?

【问题讨论】:

    标签: asp.net-mvc iis dynamic-ip


    【解决方案1】:

    第一种方式重新启动应用程序。第二种方法是在动作级别上工作(已经创建了对象)。

    因此,我在 Begin_Request 上阻止/重定向请求。我正在添加要阻止缓存的 ips。然后,如果请求 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");
                }
             }
        }
    

    ipHelper.BlackListIp() 检查 ip 是否在黑名单中。

    【讨论】:

      猜你喜欢
      • 2016-02-21
      • 1970-01-01
      • 2017-03-07
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多