Home >Topics >IIS >How to disable IP access using IIS API

How to disable IP access using IIS API

coldplay.xixi
coldplay.xixiforward
2020-12-18 17:47:387596browse

IIS InstallationThe column introduces how to use the IIS API to disable IP access

How to disable IP access using IIS API

Free recommendations: IIS installation

This class is a simple package based on Microsoft.Web.Administration:
PS: Microsoft.Web.Administration Can be searched and installed through Nuget.

public class IISAdministration
{
    private readonly ServerManager serverManager;
    public IISAdministration()
    {
        serverManager = new ServerManager();
    }

    public IEnumerable<WorkerProcess> GetWorkerProcesses()
    {
        return serverManager.WorkerProcesses;
    }

    public IEnumerable<string> GetSiteNames()
    {
        foreach (var item in GetWorkerProcesses())
        {
            yield return item.AppPoolName;
        }
    }

    public ConfigurationElementCollection GetIpSecurityCollection(string site)
    {
        return GetConfigurationElementCollection("system.webServer/security/ipSecurity", site);
    }

    public ConfigurationElementCollection GetConfigurationElementCollection(string sectionName, string site = "")
    {
        var config = serverManager.GetApplicationHostConfiguration();
        ConfigurationSection section;
        if (string.IsNullOrWhiteSpace(site))
        {
            section = config.GetSection(sectionName);
        }
        else
        {
            section = config.GetSection(sectionName, site);
        }
        return section.GetCollection();
    }

    public void CreateElement(ConfigurationElementCollection section, ConfigurationElement element)
    {
        section.Add(element);
        serverManager.CommitChanges();
    }

    public void RemoveElement(ConfigurationElementCollection section, ConfigurationElement element)
    {
        section.Remove(element);
        serverManager.CommitChanges();
    }

    public bool HasBlocked(string siteName, string ip)
    {
        var ipSecurityCollection = this.GetIpSecurityCollection(siteName);
        for (int i = 0; i < ipSecurityCollection.Count; i++)
        {
            var element = ipSecurityCollection[i];
            if ((string)element["ipAddress"] == ip)
            {
                return true;
            }
        }
        return false;
    }

    public void FreeIP(string siteName, string ip)
    {
        if (!HasBlocked(siteName, ip))
        {
            return;
        }
        var ipSecurityCollection = this.GetIpSecurityCollection(siteName);
        for (int i = 0; i < ipSecurityCollection.Count; i++)
        {
            var element = ipSecurityCollection[i];
            if ((string)element["ipAddress"] == ip)
            {
                this.RemoveElement(ipSecurityCollection, element);
                break;
            }
        }
    }

    public void BlockIP(string siteName, string ip)
    {
        if (HasBlocked(siteName, ip))
        {
            return;
        }
        var ipSecurityCollection = this.GetIpSecurityCollection(siteName);
        var element = ipSecurityCollection.CreateElement("add");
        element["ipAddress"] = ip;
        element["allowed"] = false;

        ipSecurityCollection.Add(element);
        serverManager.CommitChanges();
    }
}

Usage:

var iisAdministration = new IISAdministration();
iisAdministration.BlockIP("", "192.0.0.1");

Note:

  1. BlockIPThe first parameter is the site name. If it is an empty string, it will be used directly. Add to the IP mask under the root path of IIS.
  2. This method will throw an exception and requires administrator privileges to execute.

The above is the detailed content of How to disable IP access using IIS API. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jianshu.com. If there is any infringement, please contact admin@php.cn delete