C# IIS站点限制特定的IP操作

原创 2007年10月12日 16:28:00

参考别人的代码:

using System;
using System.DirectoryServices;
using System.Reflection;
//using mscorlib;

namespace ConfigIIS
{
 /// <summary>
 /// Small class containing methods to configure IIS.
 /// </summary>
 class class1
 {
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
   try
   {
    // retrieve the directory entry for the root of the IIS server
    System.DirectoryServices.DirectoryEntry IIS = new System.DirectoryServices.DirectoryEntry("IIS://localhost/w3svc/1/root");

    // retrieve the list of currently denied IPs
    Console.WriteLine("Retrieving the list of currently denied IPs.");

    // get the IPSecurity property
    Type typ = IIS.Properties["IPSecurity"][0].GetType();
    object IPSecurity = IIS.Properties["IPSecurity"][0];

    // retrieve the IPDeny list from the IPSecurity object
    Array origIPDenyList = (Array) typ.InvokeMember("IPDeny",
     BindingFlags.DeclaredOnly |
     BindingFlags.Public | BindingFlags.NonPublic |
     BindingFlags.Instance | BindingFlags.GetProperty, null, IPSecurity, null);

    // display what was being denied
    foreach(string s in origIPDenyList)
     Console.WriteLine("Before: " + s);

    // check GrantByDefault.  This has to be set to true, or what we are doing will not work.
    bool bGrantByDefault = (bool) typ.InvokeMember("GrantByDefault",
     BindingFlags.DeclaredOnly |
     BindingFlags.Public | BindingFlags.NonPublic |
     BindingFlags.Instance | BindingFlags.GetProperty, null, IPSecurity, null);

    Console.WriteLine("GrantByDefault = " + bGrantByDefault);
    if(!bGrantByDefault)
    {
     typ.InvokeMember("GrantByDefault",
      BindingFlags.DeclaredOnly |
      BindingFlags.Public | BindingFlags.NonPublic |
      BindingFlags.Instance | BindingFlags.SetProperty, null, IPSecurity, new object[] {true});
    }

    // update the list of denied IPs.  This is a complete replace.  If you want to maintain what
    // was already being denied, you need to make sure those IPs are in here as well.  This area
    // will be where you will most likely modify to your needs as this is just an example.
    Console.WriteLine("Updating the list of denied IPs.");
    object[] newIPDenyList = new object[4];
    newIPDenyList[0] = "192.168.1.477, 255.255.255.0";
    newIPDenyList[1] = "192.168.1.76, 255.255.255.0";
    newIPDenyList[2] = "192.168.1.467, 255.255.255.0";
    newIPDenyList[3] = "192.168.1.106, 255.255.255.0";
    Console.WriteLine("Calling SetProperty");

    // add the updated list back to the IPSecurity object
    typ.InvokeMember("IPDeny",
     BindingFlags.DeclaredOnly |
     BindingFlags.Public | BindingFlags.NonPublic |
     BindingFlags.Instance | BindingFlags.SetProperty, null, IPSecurity, new object[] {newIPDenyList});
           
    IIS.Properties["IPSecurity"][0] = IPSecurity;           
    Console.WriteLine("Commiting the changes.");

    // commit the changes
    IIS.CommitChanges();
    IIS.RefreshCache();

    // check to see if the update took
    Console.WriteLine("Checking to see if the update took.");
    IPSecurity = IIS.Properties["IPSecurity"][0];
    Array y = (Array) typ.InvokeMember("IPDeny",
     BindingFlags.DeclaredOnly |
     BindingFlags.Public | BindingFlags.NonPublic |
     BindingFlags.Instance | BindingFlags.GetProperty, null, IPSecurity, null);
    foreach(string s in y)
     Console.WriteLine("After:  " + s);
   }
   catch (Exception e)
   {
    Console.WriteLine("Error: " + e.ToString());
   }
  }
 
 }
}

 

按照上述的代码运行出现以上问题, 不知道怎么解决, 如有解决方法请留言pengbincn@gmail.com谢谢

C:/Documents and Settings/Administrator>D:/Project/ConfigIIS/ConfigIIS/bin/Debug
/ConfigIIS.exe
Retrieving the list of currently denied IPs.
GrantByDefault = True
Updating the list of denied IPs.
Calling SetProperty
Error: System.Reflection.TargetInvocationException: 调用的目标发生了异常。 --->
System.ArgumentException: 参数不正确。
   --- 内部异常堆栈跟踪的结尾 ---
   at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr,
Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[]
namedParameters)
   at System.RuntimeType.InvokeMember(String name, BindingFlags invokeAttr, Bind
er binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureI
nfo culture, String[] namedParameters)
   at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder bind
er, Object target, Object[] args)
   at ConfigIIS.class1.Main(String[] args) in d:/project/configiis/configiis/cla
ss1.cs:line 68

不过将以上代码对应的代码替换能实现单个IP的限制。

typ.InvokeMember("IPDeny",
     BindingFlags.DeclaredOnly |
     BindingFlags.Public | BindingFlags.NonPublic |
     BindingFlags.Instance | BindingFlags.SetProperty, null, IPSecurity, new object[] {"192.168.1.477, 255.255.255.0"});

谁能帮我解决上面这个问题实现IIS的多IP限制 , 请联系我, EMAIL : pengbincn@gmail.com

如何通过WEB方式,来控制iis的禁用IP名单

如何通过WEB方式,来控制iis的禁用IP名单。这个问题可以进一步划分为两个问题:1、如何控制iis的IPDeny;2、由于是WEB方式,默认的WEB帐户权限很低,不会有上面操作的权限,如何处理。 第...
  • stdotleo
  • stdotleo
  • 2004-09-07 10:47:00
  • 938

我的服务器开发之路-nginx禁止使用ip地址直接访问

nginx禁止使用ip地址直接访问
  • w_w_see
  • w_w_see
  • 2016-12-08 16:36:07
  • 4403

apache、iis6、ii7屏蔽限制ip访问(适用虚拟主机)

注:根据需求修改(8.8.4.4|8.8.8.)ip范围 Linux下 规则文件.htaccess(手工创建.htaccess文件到站点根目录) RewriteEngine On #Block i...
  • forest_fire
  • forest_fire
  • 2017-07-18 13:57:20
  • 277

C#深入操作IIS技术(创建配置站点和设置IIS Web服务扩展)

由于做的系统使用WebService+WinForm实现,所以部署的时候需要自动操作安装WebService到IIS中,并且实现对IIS的自动配置,以及设置IIS的服务扩展(需要启用ASP.NET v...
  • xukuilove
  • xukuilove
  • 2011-03-29 14:13:00
  • 2449

.net C# 程序控制IIS 添加站点域名绑定

首先添加两个引用: System.EnterpriseServices及System.DirectoryServices 然后再在代码中引用: using System.DirectoryServic...
  • hejisan
  • hejisan
  • 2017-05-04 00:08:44
  • 1322

nginx 禁止ip直接访问

添加server server { listen 80 default_server; server_name _; return 500; } 或者 serv...
  • u013372487
  • u013372487
  • 2018-02-26 18:29:16
  • 26

如何阻止某个IP访问我的电脑-用本地安全策略限制某个IP段

方法一:通过windows自带的安全策略限制 打开本地安全设置,点“IP安全策略,在本地机器”——>创建IP安全策略---->下一步---->名称随便写,如输入阻止,然后一直点下一步,出现提示点是,...
  • saga_gallon
  • saga_gallon
  • 2014-06-17 17:22:40
  • 3479

通过IIS设置阻止某个IP或IP段访问你的网站

最近笔者发现,我的一个网站每天只要一发布新文章,就被一“黑爪”立即转走了,并且大肆在其他网站转发,这样时间久了难免会造成网站内容质量的下降,甚至影响整个网站的权重,在这样的情况下,我就想阻止他访问我网...
  • oKeVin12
  • oKeVin12
  • 2015-07-10 14:25:04
  • 4233

[C#]_[使用脚本启动和停止IIS站点]

1.在开发asp.net网站时,自动化部署网站虽然可以替换dll立即生效(这点比Java网站强).但是总有需要停止和启动的时候,比如 -- 修改了网站配置文件, -- 或者严格意义上的商业站点上的库版...
  • infoworld
  • infoworld
  • 2012-11-29 00:04:32
  • 2344

IIS安装教程,IIS配置,及解决80端口被屏蔽和动态IP甚至无公网IP问题

IIS,IIS配置,IIS安装教程,
  • iiroj
  • iiroj
  • 2014-07-16 21:40:27
  • 1029
收藏助手
不良信息举报
您举报文章:C# IIS站点限制特定的IP操作
举报原因:
原因补充:

(最多只允许输入30个字)