簡體   English   中英

如何使用C#為sql server express的windows防火牆添加異常

[英]How can I add exception to windows firewall for sql server express using C#

我想使用C#為sql server express 2008 R2的Windows防火牆添加例外。

我怎樣才能做到這一點?

場景:

  • 我有一個與Sql Server 2008 express R2數據庫一起安裝的應用程序,
  • 我想在防火牆中添加異常,以便其他用戶可以連接到數據庫。 (在sql express的配置文件中啟用了TCP)

如何使用C#完成此操作,我想在第一個應用程序啟動時或在使用自定義操作的設置過程中自動執行此操作。

在使用Windows防火牆的其他整潔的東西中可以很容易地在C#中執行此操作,有關更多信息,請參閱此文章控制Windows防火牆C#

您還可以查看此處發布的答案。

試試這個鏈接 它包含了對Windows防火牆API的訪問。

應該做的伎倆。

編輯:

實際上這看起來更好 - > 用C#自動化Windows防火牆設置

你應該能夠使用它打開你想要的端口

1: INetFwOpenPorts ports; 
2: INetFwOpenPort port; 
3: port.Port = 1433; /* port no */
4: port.Name = “Application1”; /*name of the application using the port */
5: port.Enabled =  true; /* enable the port */
6: /*other properties like Protocol, IP Version can also be set accordingly
7: now add this to the GloballyOpenPorts collection */
8: 
9: Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); 
10: INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType); 
11: ports = (INetFwOpenPorts)mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts; 
12: 
13: ports.Add(port);
14: 

請注意,在Visual Studio中,您需要將NetFwTypeLib COM引用添加到項目中,並在項目中包含NetFwTypeLib( using NetFwTypeLib;

以下是一些選項:

Windows XP SP2防火牆控制器

http://www.codeproject.com/KB/winsdk/WinXPSP2Firewall.aspx

通過COM Interop使用C#控制Windows防火牆

http://www.shafqatahmed.com/2008/01/controlling-win.html

Netsh防火牆上下文的Netsh命令語法

http://technet.microsoft.com/en-us/library/bb490617.aspx

在這里,您可以為具有多配置文件域,公共和私有通過C#的sql server 1433端口創建入站防火牆規則。

首先,您必須從系統“C:\\ Windows \\ System32 / FirewallAPI.dll”導入dll。 將此DLL添加到項目引用中。 轉到visual studio中的解決方案資源管理器 - >引用 - >添加引用 - >單擊確定附近的瀏覽按鈕並取消按鈕 - >瀏覽路徑C:\\ Windows \\ System32 / FirewallAPI.dll並添加dll。

在上面的程序中添加dll后,在你的代碼中使用命名空間“NetFwTypeLib”。 使用NetFwTypeLib;

   using NetFwTypeLib;
   namespace ConsoleAppTestDemo
   {
    class Program
    {
     static void Main(string[] args)
     {
        Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
        INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);

        // Let's create a new rule
        INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
        inboundRule.Enabled = true;
        //Allow through firewall
        inboundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;

        //For all profile
        inboundRule.Profiles = (int)NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_ALL;

        //Using protocol TCP
        inboundRule.Protocol = 6; // TCP
        //Local Port 1433
        inboundRule.LocalPorts = "1433";
        //Name of rule
        inboundRule.Name = "SQLRule";

        // Now add the rule
        INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
        firewallPolicy.Rules.Add(inboundRule);
    }
}

}

之后,您可以檢查防火牆入站規則。 在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM