public static void RemoveIpRestriction(IisSite info, IpRestriction restriction) { using (var serverManager = ServerManager.OpenRemote(info.Hostname)) { var hostConfig = serverManager.GetApplicationHostConfiguration(); // Add IP restrictions var ipSec = hostConfig.GetSection("system.webServer/security/ipSecurity", info.Sitename); var ipSecCollection = ipSec.GetCollection(); foreach (var element in ipSecCollection.Where(element => (string)element.Attributes["ipAddress"].Value == restriction.Ip && (string.IsNullOrEmpty(restriction.Netmask) || (string)element.Attributes["subnetMask"].Value == restriction.Netmask))) { ipSecCollection.Remove(element); break; } // Save changes serverManager.CommitChanges(); } }
public static void AddIpRestriction(IisSite info, IpRestriction restriction) { using (var serverManager = ServerManager.OpenRemote(info.Hostname)) { var hostConfig = serverManager.GetApplicationHostConfiguration(); // Add IP restrictions var ipSec = hostConfig.GetSection("system.webServer/security/ipSecurity", info.Sitename); var ipSecCollection = ipSec.GetCollection(); var ipElement = ipSecCollection.CreateElement("add"); ipElement["ipAddress"] = restriction.Ip; ipElement["allowed"] = false; if (!string.IsNullOrEmpty(restriction.Netmask)) { ipElement["subnetMask"] = restriction.Netmask; } ipSecCollection.Add(ipElement); // Save changes serverManager.CommitChanges(); } }