2009-02-26 21 views
5

我正在創建基於論壇的網站,並希望阻止發佈垃圾郵件或濫用的成員。我正在考慮使用HTTPModule來做到這一點,但我遇到了IIS7的動態IP限制擴展。我想知道是否可以從我的應用動態地將IP添加到擴展中?我是否可以通過ASP.NET應用程序以編程方式將IP地址添加到IIS7中的動態IP限制擴展?

另外,如果你有這個擴展的經驗,這將是偉大的。我特別。有興趣知道它是否會影響高流量網站的表現。

感謝

回答

3

我也有興趣在此。

起初,我使用IIS7中的UI將IP地址列入黑名單。

enter image description here

我也看看上面提到的裏克施特拉爾鏈接,但在這裏找到一個很好的資源:

http://www.iis.net/configreference/system.webserver/security/ipsecurity/add

該網頁上的代碼示例說明如何執行的動作使用C#。下面是從該網站

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(); 
     } 
    } 
} 

的SNIP要獲得Microsoft.Web.Administration包,在Visual Studio中轉到工具 - > NuGet包管理器 - >軟件包管理器控制檯。

然後鍵入:

Install-Package Microsoft.Web.Administration 

執行相同的任務的另一種方法是使用命令行和APPCMD命令。

下面的命令做同樣的事情:

appcmd.exe set config "Default Web Site/SSM" -section:system.webServer/security/ipSecurity /+"[ipAddress='192.168.100.1',allowed='False']" /commit:apphost 

,可以從代碼中使用被稱爲:

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#調用它,它抱怨說「系統找不到指定的文件異常」。爲了解決這個問題,你必須提供管理員用戶名/密碼。

下面是函數:

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瞧!

相關問題