Programmatically edit an IIS IPGrant table

Hi

I am working on a software solution for editing an IPGrant table in IIS.

As I said, I can view the IPGrant list correctly and add to it.

However, I cannot remove or replace items in the IPGrant list.

MSDN and I recommend that you write (the values ​​of the old list + the new value) in List, however I find that I get HResult from "Cannot create file with this name, file already exists", adding to the list only works for me If I transfer only the new value .

After some reading:

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

I found that the compatibility issue with IIS 7/6 and the use of Metabase is that you can only add to it and not remove it.

IIS 7/7.5, ( #, ) IPGrant.

+3
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 = 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();

FindElement (, elementTagName, valuesToMatch) {   for (var = 0; < collection.Count; ++) {       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

+1

Source: https://habr.com/ru/post/1772484/


All Articles