加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

我可以通过编程方式从我的ASP.NET应用程序向IIS7中的动态IP限制

发布时间:2020-12-16 07:07:27 所属栏目:asp.Net 来源:网络整理
导读:我正在创建一个基于论坛的网站,并希望阻止发布垃圾邮件或滥用的成员.我正在考虑使用HTTPModule来执行此操作,但我遇到了IIS7的动态IP限制扩展.我想知道是否可以从我的应用程序动态添加IP到扩展名? 此外,如果您有该扩展的经验,这将是伟大的.我,尤其是.有兴趣
我正在创建一个基于论坛的网站,并希望阻止发布垃圾邮件或滥用的成员.我正在考虑使用HTTPModule来执行此操作,但我遇到了IIS7的动态IP限制扩展.我想知道是否可以从我的应用程序动态添加IP到扩展名?

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

谢谢

解决方法

我也对此感兴趣.

起初我使用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();
      }
   }
}

要获取Microsoft.Web.Administration包,请在visual studio中转到工具 – > Nuget包管理器 – >包管理器控制台.

然后输入:

Install-Package Microsoft.Web.Administration

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

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

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

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

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

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

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

这是功能:

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}',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:windowsSystem32inetsrvappcmd.exe",args,"Administrator",password,domain);
}

Et Voila!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读