以编程方式编辑IIS IPGrant表

3
我一直在研究通过编程方式编辑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

我发现在使用Metabase时,IIS 7/6存在兼容性问题 - 只能添加到它,而不能删除。

是否有更为当前的方法可用于管理IPGrant表格的IIS 7/7.5(请使用c#)。

1个回答

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 "未找到元素!";

ipSecurityCollection.DeleteElement(addElementPos);

adminManager.CommitChanges();

function FindElement(collection, elementTagName, valuesToMatch) { for (var i = 0; i < collection.Count; i++) { var element = collection.Item(i);

    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 set config -section:system.webServer/security/ipSecurity /-"[ipAddress='169.132.124.234',subnetMask='255.255.255.255',domainName='']" /commit:apphost


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接