我可以通过编程方式从我的ASP.NET应用程序向IIS7中的动态IP限制扩展添加IP地址吗?

Wal*_*ssa 5 asp.net iis iis-7

我正在创建一个基于论坛的网站,并希望阻止发布垃圾邮件或滥用的成员.我正在考虑使用HTTPModule来执行此操作,但我遇到了IIS7的动态IP限制扩展.我想知道是否可以从我的应用程序动态添加IP到扩展名?

此外,如果您有该扩展的经验,这将是伟大的.我,尤其是.有兴趣知道它是否会影响高流量网站的性能.

谢谢

Oce*_*rop 7

我也对这个很感兴趣。

起初我使用 IIS7 中的 UI 将 IP 地址列入黑名单。

在此处输入图片说明

我确实查看了上面提到的 Rick Strahl 链接,但在这里找到了一个很好的资源:

http://www.iis.net/configreference/system.webserver/security/ipsecurity/add

该页面上的代码示例向您展示了如何使用 C# 执行操作。这是该网站的截图

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();
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

要获取 Microsoft.Web.Administration 包,请在 Visual Studio 中转到 Tools -> Nuget Package Manager -> Package Manager Console。

然后输入:

Install-Package Microsoft.Web.Administration
Run Code Online (Sandbox Code Playgroud)

执行相同任务的另一种方法是使用命令行和 appcmd 命令。

以下命令执行相同的操作:

appcmd.exe set config "Default Web Site/SSM" -section:system.webServer/security/ipSecurity /+"[ipAddress='192.168.100.1',allowed='False']" /commit:apphost
Run Code Online (Sandbox Code Playgroud)

并且可以使用以下代码从代码中调用:

string website = "Default Web Site/SSM";
string ipAddress = "192.168.100.1";
string allowDeny = "False";

string cmd = string.Format("%systemroot%\\system32\\inetsrv\\appcmd.exe set config \"{0}\" -section:system.webServer/security/ipSecurity /+\"[ipAddress='{1}',allowed='{2}']\" /commit:apphost", website, ipAddress, allowDeny);
Process.Start(cmd);
Run Code Online (Sandbox Code Playgroud)

上面的命令有效,但如果你从 C# 调用它,它会抱怨说“系统找不到指定的异常文件”。要解决这个问题,您必须提供管理员用户名/密码。

这是函数:

void BlacklistIP(string ipAddress)
{
    string website = "Default Web Site/SSM";
    string allowDeny = "False";
    string domain = "";

    string args = string.Format(" set config \"{0}\" -section:system.webServer/security/ipSecurity /+\"[ipAddress='{1}',allowed='{2}']\" /commit:apphost", website, ipAddress, allowDeny);

    System.Security.SecureString password = new System.Security.SecureString();
    password.AppendChar('y');
    password.AppendChar('o');
    password.AppendChar('u');
    password.AppendChar('r');
    password.AppendChar('p');
    password.AppendChar('a');
    password.AppendChar('s');
    password.AppendChar('s');
    password.AppendChar('w');
    password.AppendChar('o');
    password.AppendChar('r');
    password.AppendChar('d');

    Process.Start(@"C:\windows\System32\inetsrv\appcmd.exe", args, "Administrator", password, domain);
}
Run Code Online (Sandbox Code Playgroud)

等等!


REA*_*REW 4

看起来 Rick Strahl 已经使用 IIS API 实现了这一点,请参阅下面的链接:

http://www.west-wind.com/WebLog/posts/59731.aspx

安德鲁