您可以使用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