首页 > 专题 > IIS > 正文

如何使用IIS API禁用IP访问

coldplay.xixi
发布: 2020-12-18 17:47:38
转载
7267 人浏览过

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学习者快速成长!