您的位置:首页 > 编程语言 > C#

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

2007-10-12 16:28 399 查看
参考别人的代码:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iis c# object string null list