Home > Topics > IIS > body text

How to disable IP access using IIS API

coldplay.xixi
Release: 2020-12-18 17:47:38
forward
7228 people have browsed it

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();
    }
}
Copy after login

Usage:

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

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!

Related labels:
source:jianshu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!