2009-02-26 5 views
5

Tôi đang tạo trang web dựa trên diễn đàn và muốn chặn các thành viên đăng spam hoặc lạm dụng. Tôi đã suy nghĩ về việc sử dụng một HTTPModule để làm điều này, nhưng tôi đã đi qua phần mở rộng Dynamic IP Restrictions đến IIS7. Tôi tự hỏi liệu có thể thêm IP động từ ứng dụng của tôi vào tiện ích mở rộng không?Tôi có thể lập trình thêm địa chỉ IP vào phần mở rộng Hạn chế IP động trong IIS7 từ ứng dụng ASP.NET của tôi không?

Ngoài ra, nếu bạn có kinh nghiệm với tiện ích đó, điều này sẽ rất tuyệt vời. Tôi là đặc biệt. quan tâm để biết liệu nó có thể ảnh hưởng đến hiệu suất trong một trang web lưu lượng truy cập cao hay không.

Cảm ơn

Trả lời

3

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.

enter image description here

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!