2010-11-01 133 views
3

HI,以编程方式编辑IIS IPGrant表

我一直在编写一个编程解决方案来编辑​​IIS中的IPGrant表。

现在,我可以正确查看IPGrant列表,并可以添加到它。

但是,我无法删除或替换IPGrant列表中的项目。

MSDN和这样的建议,你写(清单的值+新值)的列表,但我发现我得到一个HResult“无法创建文件的名称,文件已经存在”。 添加到列表仅适用于我如果我只传入新值。

一些阅读后:

http://www.west-wind.com/weblog/posts/59731.aspx 
http://www.aspdev.org/articles/web.config/ 
http://www.codeproject.com/KB/security/iiswmi.aspx 
http://www.codeproject.com/KB/security/iiswmi.aspx?msg=1739049 
http://blogs.msdn.com/b/shawnfa/archive/0001/01/01/400749.aspx 
http://msdn.microsoft.com/en-us/library/ms524322%28VS.90%29.aspx 
http://www.eggheadcafe.com/software/aspnet/33215307/setting-ip-restrictions-in-iis-7.aspx 

我发现有一个兼容性问题与IIS 7/6,并使用元数据库 - 在一个只能添加到它,而不是删除。

是否有一个更新的IIS7/7.5的方法可以使用(在C#中)来管理IPGrant表。

回答

1

您可以使用Microsoft.Web.Administration,或APPCMD或JavaScript(COM - AHADMIN)要做到这一点,这里是关于如何去除几个例子:

private static void Main() { 

    using(ServerManager serverManager = new ServerManager()) { 
     Configuration config = serverManager.GetApplicationHostConfiguration(); 

     ConfigurationSection ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity"); 

     ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection(); 

     ConfigurationElement addElement = FindElement(ipSecurityCollection, "add", "ipAddress", @"169.132.124.234", "subnetMask", @"255.255.255.255", "domainName", @""); 
     if (addElement == null) throw new InvalidOperationException("Element not found!"); 

     ipSecurityCollection.Remove(addElement); 

     serverManager.CommitChanges(); 
    } 
} 

private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues) { 
    foreach (ConfigurationElement element in collection) { 
     if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase)) { 
      bool matches = true; 

      for (int i = 0; i < keyValues.Length; i += 2) { 
       object o = element.GetAttributeValue(keyValues[i]); 
       string value = null; 
       if (o != null) { 
        value = o.ToString(); 
       } 

       if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase)) { 
        matches = false; 
        break; 
       } 
      } 
      if (matches) { 
       return element; 
      } 
     } 
    } 
    return null; 
} 

使用Javascript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager'); adminManager.CommitPath =“MACHINE/WEBROOT/APPHOST”;

var ipSecuritySection = adminManager.GetAdminSection(“system.webServer/security/ipSecurity”,“MACHINE/WEBROOT/APPHOST”);

var ipSecurityCollection = ipSecuritySection.Collection; var addElementPos = FindElement(ipSecurityCollection,“add”,[“ipAddress”,“169.132.124.234”,“subnetMask”,“255.255.255.255”,“domainName”,“”]); if(addElementPos == -1)throw“Element not found!”;

ipSecurityCollection.DeleteElement(addElementPos);

adminManager.CommitChanges(); { VAR元素= collection.Item(i)用于(;我< collection.Count我+ + VAR I = 0)

功能FindElement(收集,elementTagName,valuesToMatch){ ;

if (element.Name == elementTagName) { 
     var matches = true; 
     for (var iVal = 0; iVal < valuesToMatch.length; iVal += 2) { 
      var property = element.GetPropertyByName(valuesToMatch[iVal]); 
      var value = property.Value; 
      if (value != null) { 
       value = value.toString(); 
      } 
      if (value != valuesToMatch[iVal + 1]) { 
       matches = false; 
       break; 
      } 
     } 
     if (matches) { 
      return i; 
     } 
    } 
} 

return -1; 

}

终于Appcmd.exe的:
Appcmd.exe的设置配置-section:system.webServer /安全/ IPSECURITY /-"[ipAddress='169.132.124.234',subnetMask=” 255.255.255.255',domainName ='']“/ commit:apphost