Heim  >  Artikel  >  Themen  >  So deaktivieren Sie den IP-Zugriff mithilfe der IIS-API

So deaktivieren Sie den IP-Zugriff mithilfe der IIS-API

coldplay.xixi
coldplay.xixinach vorne
2020-12-18 17:47:387270Durchsuche

IIS-InstallationDie Kolumne stellt vor, wie man die IIS-API verwendet, um den IP-Zugriff zu deaktivieren

So deaktivieren Sie den IP-Zugriff mithilfe der IIS-API

Kostenlose Empfehlung: IIS-Installation

Dieser Kurs basiert auf Microsoft.Web.Administration code> Ein einfaches Paket:<br>PS: <code>Microsoft.Web.Administration kann über Nuget gesucht und installiert werden. Microsoft.Web.Administration 写的一个简单封装:
PS: Microsoft.Web.Administration 可通过 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();
    }
}

使用方法:

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

注意:

  1. BlockIP第一个参数为站点名,如果空字符串,则直接添加到 IISrrreee
  2. Verwendung:
  3. rrreee
  4. Hinweis:
    🎜BlockIPDer erste Parameter ist der Site-Name, er wird direkt zum IIS Root-IP-Blockierung unter dem Pfad. 🎜🎜Diese Methode löst eine Ausnahme aus und erfordert zur Ausführung Administratorrechte. 🎜🎜

Das obige ist der detaillierte Inhalt vonSo deaktivieren Sie den IP-Zugriff mithilfe der IIS-API. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:jianshu.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen