【求助】C# IIS站点限制特定的IP操作

本文档介绍了一个使用C#配置IIS站点以限制特定IP访问的问题。代码试图通过DirectoryServices更新IPDeny列表,但在批量设置新IP时遇到参数不正确的异常。已知单个IP限制可以正常工作,但尝试添加多个IP时失败。寻求解决方案,如有帮助请发送邮件至pengbincn@gmail.com。
摘要由CSDN通过智能技术生成

参考别人的代码:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值