【发布时间】:2015-03-31 11:46:29
【问题描述】:
我想为 MVC 5 项目添加/删除运行时的 IP 限制。
我搜索了一下,找到了两种方法。
-
在运行时更改动态 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或应用程序?
我将为此使用节流。
如果应用程序或 IIS 尚未重新启动,我更喜欢第一种方式,因为它在 IIS 级别。
您有什么建议最好的方法或其他方法吗?
【问题讨论】:
标签: asp.net-mvc iis dynamic-ip