I am trying to write a console app to block access to everyone except a specific IP address and have the code below which adds and entry into the Allow list, but global access remains as allow. How can I make it so that global access is denied?
static void Main(string[] args)
{
try
{
Console.WriteLine("Website to alter: ");
string websiteToAlter = Console.ReadLine();
Console.WriteLine("Enter IP Address ranges allowed access (Separate with comma) : ");
string ipAddresses = Console.ReadLine();
DirectoryEntry IIS = new DirectoryEntry("IIS://Localhost/w3svc/1/Root/" + websiteToAlter);
IIS.RefreshCache();
object IPSecurity = IIS.Properties["IPSecurity"].Value;
object objGrantedByDefault = false;
object[] newIPDenyList = new object[1];
newIPDenyList[0] = ipAddresses;
IPSecurity.GetType().InvokeMember("GrantByDefault",
BindingFlags.Public |
BindingFlags.Instance | BindingFlags.SetProperty,
null, IPSecurity, new object[] { false });
IPSecurity.GetType().InvokeMember("IPGrant",
BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty, null, IPSecurity,
new object[] { newIPDenyList });
IIS.Properties["IPSecurity"].Value = IPSecurity;
IIS.CommitChanges();
IIS.RefreshCache();
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.ToString());
}
}