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.Reflecti on.TargetInvoca tionException
to be thrown. Here is the code that adds the addresses:
static void SetIPSecurityPr operty(string metabasePath, string member,
string item)
{
try
{
if (("IPGrant" != member) && ("IPDeny" != member) &&
("DomainGran t" != member) && ("DomainDeny " != member))
{
Console.WriteLi ne(" Failed in SetIPSecurityPr operty; second
param must be one of IPGrant|IPDeny| DomainGrant|Dom ainDeny");
}
else
{
DirectoryEntry path = new DirectoryEntry( metabasePath);
path.RefreshCac he();
object ipsecObj = path.Invoke("Ge t", new string[] {
"IPSecurity " });
Type t = ipsecObj.GetTyp e();
Array data = (Array)t.Invoke Member(member, BindingFlags.
GetProperty, null, ipsecObj, null);
Console.WriteLi ne(item.ToStrin g());
bool exists = false;
foreach (object dataItem in data)
{
//Console.WriteLi ne(" {0}", dataItem.ToStri ng());
if (dataItem.ToStr ing().StartsWit h(item))
{
exists = true;
}
}
if (exists)
{
//Console.WriteLi ne(" {0} already exists in {1}", item,
member);
}
else
{
object[] newData = new object[data.Length + 1];
data.CopyTo(new Data, 0);
newData.SetValu e(item, data.Length);
//THE EXCEPTION IS THROWN FROM THE FOLLOWING CALL:
t.InvokeMember( member, BindingFlags.Se tProperty, null,
ipsecObj, new object[] { newData });
path.Invoke("Pu t", new object[] { "IPSecurity ", ipsecObj }
);
path.CommitChan ges();
path.RefreshCac he();
ipsecObj = path.Invoke("Ge t", new string[] { "IPSecurity "
});
data = (Array)t.Invoke Member(member, BindingFlags.
GetProperty, null, ipsecObj, null);
Console.WriteLi ne(item.ToStrin g() + " added. There are
now " + data.Length + " blocked IP entries.");
//Console.WriteLi ne(" 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!
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.Reflecti on.TargetInvoca tionException
to be thrown. Here is the code that adds the addresses:
static void SetIPSecurityPr operty(string metabasePath, string member,
string item)
{
try
{
if (("IPGrant" != member) && ("IPDeny" != member) &&
("DomainGran t" != member) && ("DomainDeny " != member))
{
Console.WriteLi ne(" Failed in SetIPSecurityPr operty; second
param must be one of IPGrant|IPDeny| DomainGrant|Dom ainDeny");
}
else
{
DirectoryEntry path = new DirectoryEntry( metabasePath);
path.RefreshCac he();
object ipsecObj = path.Invoke("Ge t", new string[] {
"IPSecurity " });
Type t = ipsecObj.GetTyp e();
Array data = (Array)t.Invoke Member(member, BindingFlags.
GetProperty, null, ipsecObj, null);
Console.WriteLi ne(item.ToStrin g());
bool exists = false;
foreach (object dataItem in data)
{
//Console.WriteLi ne(" {0}", dataItem.ToStri ng());
if (dataItem.ToStr ing().StartsWit h(item))
{
exists = true;
}
}
if (exists)
{
//Console.WriteLi ne(" {0} already exists in {1}", item,
member);
}
else
{
object[] newData = new object[data.Length + 1];
data.CopyTo(new Data, 0);
newData.SetValu e(item, data.Length);
//THE EXCEPTION IS THROWN FROM THE FOLLOWING CALL:
t.InvokeMember( member, BindingFlags.Se tProperty, null,
ipsecObj, new object[] { newData });
path.Invoke("Pu t", new object[] { "IPSecurity ", ipsecObj }
);
path.CommitChan ges();
path.RefreshCac he();
ipsecObj = path.Invoke("Ge t", new string[] { "IPSecurity "
});
data = (Array)t.Invoke Member(member, BindingFlags.
GetProperty, null, ipsecObj, null);
Console.WriteLi ne(item.ToStrin g() + " added. There are
now " + data.Length + " blocked IP entries.");
//Console.WriteLi ne(" 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!
Comment