Problem using InvokeMember() to add blocked IP addresses to IIS

T

tommyk

I am writing a small program that reads a text file filled with a large
number of IP addresses and attempts to add them to IIS so that they will be
blocked. For some reason, about one in every five or six ip addresses that I
attempt to add to IIS causes an System.Reflection.TargetInvocationException
to be thrown. Here is the code that adds the addresses:

static void SetIPSecurityProperty(string metabasePath, string member,
string item)
{
try
{
if (("IPGrant" != member) && ("IPDeny" != member) &&
("DomainGrant" != member) && ("DomainDeny" != member))
{
Console.WriteLine(" Failed in SetIPSecurityProperty; second
param must be one of IPGrant|IPDeny|DomainGrant|DomainDeny");
}
else
{
DirectoryEntry path = new DirectoryEntry(metabasePath);
path.RefreshCache();
object ipsecObj = path.Invoke("Get", new string[] {
"IPSecurity" });
Type t = ipsecObj.GetType();
Array data = (Array)t.InvokeMember(member, BindingFlags.
GetProperty, null, ipsecObj, null);
Console.WriteLine(item.ToString());
bool exists = false;
foreach (object dataItem in data)
{
//Console.WriteLine(" {0}", dataItem.ToString());
if (dataItem.ToString().StartsWith(item))
{
exists = true;
}
}

if (exists)
{
//Console.WriteLine(" {0} already exists in {1}", item,
member);
}
else
{
object[] newData = new object[data.Length + 1];
data.CopyTo(newData, 0);
newData.SetValue(item, data.Length);
//THE EXCEPTION IS THROWN FROM THE FOLLOWING CALL:
t.InvokeMember(member, BindingFlags.SetProperty, null,
ipsecObj, new object[] { newData });
path.Invoke("Put", new object[] { "IPSecurity", ipsecObj }
);
path.CommitChanges();

path.RefreshCache();
ipsecObj = path.Invoke("Get", new string[] { "IPSecurity"
});
data = (Array)t.InvokeMember(member, BindingFlags.
GetProperty, null, ipsecObj, null);
Console.WriteLine(item.ToString() + " added. There are
now " + data.Length + " blocked IP entries.");
//Console.WriteLine(" New {0} =", member);
}


An ip address (and subnet) of "64.126.128.0, 255.255.128.0" has no problem
being added, but when I try an ip address such as "64.126.192.0, 255.255.128.
0" the InvokeMember (SetProperty) function throws and exception. If I open
up IIS and go to the properties of my web site and manually add "64.126.192.0,
255.255.128.0" there is no problem. Anyone have any ideas? This is driving
me nuts.

Thanks in advance!
 
H

hdgreetings.com ecards

Exact same prob here - works except with subnet added.

Please post if you find anything.
 
Top