首頁 > 專題 > IIS > 主體

如何使用IIS API禁用IP訪問

coldplay.xixi
發布: 2020-12-18 17:47:38
轉載
7207 人瀏覽過

IIS安裝欄位介紹如何使用IIS API停用IP存取

如何使用IIS API禁用IP訪問

免費推薦: IIS安裝

這個類別是基於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第一個參數為網站名,如果空字串,則直接新增到IIS 根路徑下的IP屏蔽。
  2. 此方法會拋出異常,而且需要管理員權限才能執行。

以上是如何使用IIS API禁用IP訪問的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:jianshu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!