Tôi cũng quan tâm đến việc này.
Lúc đầu, tôi đã sử dụng giao diện người dùng trong IIS7 để liệt kê địa chỉ IP.
tôi đã có một cái nhìn tại liên kết Rick Strahl nêu trên nhưng không tìm thấy một nguồn lực lớn ở đây:
http://www.iis.net/configreference/system.webserver/security/ipsecurity/add
Các mẫu mã trên trang đó cho bạn thấy làm thế nào để thực hiện hành động sử dụng C#. Đây là snip từ trang web đó
using System;
using System.Text;
using Microsoft.Web.Administration;
internal static class Sample
{
private static void Main()
{
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity", "Default Web Site");
ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection();
ConfigurationElement addElement = ipSecurityCollection.CreateElement("add");
addElement["ipAddress"] = @"192.168.100.1";
addElement["allowed"] = false;
ipSecurityCollection.Add(addElement);
ConfigurationElement addElement1 = ipSecurityCollection.CreateElement("add");
addElement1["ipAddress"] = @"169.254.0.0";
addElement1["subnetMask"] = @"255.255.0.0";
addElement1["allowed"] = false;
ipSecurityCollection.Add(addElement1);
serverManager.CommitChanges();
}
}
}
Để lấy gói Microsoft.Web.Administration, trong studio ảnh goto Tools -> Nuget Package Manager -> Package Manager Console.
Sau đó gõ:
Install-Package Microsoft.Web.Administration
Một cách khác để thực hiện các nhiệm vụ tương tự là sử dụng dòng lệnh và lệnh appcmd.
Lệnh sau làm điều tương tự:
appcmd.exe set config "Default Web Site/SSM" -section:system.webServer/security/ipSecurity /+"[ipAddress='192.168.100.1',allowed='False']" /commit:apphost
và có thể được gọi là từ mã sử dụng:
string website = "Default Web Site/SSM";
string ipAddress = "192.168.100.1";
string allowDeny = "False";
string cmd = string.Format("%systemroot%\\system32\\inetsrv\\appcmd.exe set config \"{0}\" -section:system.webServer/security/ipSecurity /+\"[ipAddress='{1}',allowed='{2}']\" /commit:apphost", website, ipAddress, allowDeny);
Process.Start(cmd);
Các công trình lệnh trên nhưng nó quay ra nếu bạn gọi nó từ C# nó than phiền nói "Hệ thống không thể tìm thấy tệp được chỉ định Ngoại lệ". Để khắc phục điều đó, bạn phải cung cấp tên người dùng/mật khẩu quản trị viên.
Dưới đây là các chức năng:
void BlacklistIP(string ipAddress)
{
string website = "Default Web Site/SSM";
string allowDeny = "False";
string domain = "";
string args = string.Format(" set config \"{0}\" -section:system.webServer/security/ipSecurity /+\"[ipAddress='{1}',allowed='{2}']\" /commit:apphost", website, ipAddress, allowDeny);
System.Security.SecureString password = new System.Security.SecureString();
password.AppendChar('y');
password.AppendChar('o');
password.AppendChar('u');
password.AppendChar('r');
password.AppendChar('p');
password.AppendChar('a');
password.AppendChar('s');
password.AppendChar('s');
password.AppendChar('w');
password.AppendChar('o');
password.AppendChar('r');
password.AppendChar('d');
Process.Start(@"C:\windows\System32\inetsrv\appcmd.exe", args, "Administrator", password, domain);
}
Et Voila!