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

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:
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. 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!
How to solve iis cannot start
Get window handle method
IIS unexpected error 0x8ffe2740 solution
The difference between rest api and api
What should I do if iis cannot start?
The difference between vscode and visual studio
The difference between wildcard masking and unmasking
Introduction to python higher-order functions