2008-09-22 11 views
20

पर प्रोग्रामेटिक रूप से एक एप्लिकेशन जोड़ें मेरे पास एक ऐसा एप्लिकेशन है जो क्लिकऑन के माध्यम से स्थापित और अपडेट किया गया है। एप्लिकेशन एफ़टीपी के माध्यम से फाइल डाउनलोड करता है, और इसलिए विंडोज फ़ायरवॉल के अपवाद के रूप में जोड़ा जाना चाहिए। क्लिकऑन काम करने के तरीके के कारण, EXE का पथ प्रत्येक अद्यतन के साथ बदलता है, इसलिए अपवाद को भी बदलने की आवश्यकता है। फ़ायरवॉल में किए गए परिवर्तनों का सबसे अच्छा तरीका क्या होगा ताकि यह अंतिम उपयोगकर्ता को अदृश्य हो?विंडोज फ़ायरवॉल

(आवेदन सी ​​# में लिखा है)

+0

मुझे एक ही समस्या है लेकिन अन्य सुरक्षा सॉफ्टवेयर (केवल विंडोज फ़ायरवॉल नहीं) – Jamiegs

उत्तर

9

मुझे यह आलेख मिला, जिसमें एक पूर्ण रैपर वर्ग है जो विंडोज फ़ायरवॉल में हेरफेर करने के लिए शामिल है। Adding an Application to the Exception list on the Windows Firewall

/// 

/// Allows basic access to the windows firewall API. 
/// This can be used to add an exception to the windows firewall 
/// exceptions list, so that our programs can continue to run merrily 
/// even when nasty windows firewall is running. 
/// 
/// Please note: It is not enforced here, but it might be a good idea 
/// to actually prompt the user before messing with their firewall settings, 
/// just as a matter of politeness. 
/// 

/// 
/// To allow the installers to authorize idiom products to work through 
/// the Windows Firewall. 
/// 
public class FirewallHelper 
{ 
    #region Variables 
    /// 

    /// Hooray! Singleton access. 
    /// 

    private static FirewallHelper instance = null; 

    /// 

    /// Interface to the firewall manager COM object 
    /// 

    private INetFwMgr fwMgr = null; 
    #endregion 
    #region Properties 
    /// 

    /// Singleton access to the firewallhelper object. 
    /// Threadsafe. 
    /// 

    public static FirewallHelper Instance 
    { 
     get 
     { 
      lock (typeof(FirewallHelper)) 
      { 
       if (instance == null) 
        instance = new FirewallHelper(); 
       return instance; 
      } 
     } 
    } 
    #endregion 
    #region Constructivat0r 
    /// 

    /// Private Constructor. If this fails, HasFirewall will return 
    /// false; 
    /// 

    private FirewallHelper() 
    { 
     // Get the type of HNetCfg.FwMgr, or null if an error occurred 
     Type fwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); 

     // Assume failed. 
     fwMgr = null; 

     if (fwMgrType != null) 
     { 
      try 
      { 
       fwMgr = (INetFwMgr)Activator.CreateInstance(fwMgrType); 
      } 
      // In all other circumnstances, fwMgr is null. 
      catch (ArgumentException) { } 
      catch (NotSupportedException) { } 
      catch (System.Reflection.TargetInvocationException) { } 
      catch (MissingMethodException) { } 
      catch (MethodAccessException) { } 
      catch (MemberAccessException) { } 
      catch (InvalidComObjectException) { } 
      catch (COMException) { } 
      catch (TypeLoadException) { } 
     } 
    } 
    #endregion 
    #region Helper Methods 
    /// 

    /// Gets whether or not the firewall is installed on this computer. 
    /// 

    /// 
    public bool IsFirewallInstalled 
    { 
     get 
     { 
      if (fwMgr != null && 
        fwMgr.LocalPolicy != null && 
        fwMgr.LocalPolicy.CurrentProfile != null) 
       return true; 
      else 
       return false; 
     } 
    } 

    /// 

    /// Returns whether or not the firewall is enabled. 
    /// If the firewall is not installed, this returns false. 
    /// 

    public bool IsFirewallEnabled 
    { 
     get 
     { 
      if (IsFirewallInstalled && fwMgr.LocalPolicy.CurrentProfile.FirewallEnabled) 
       return true; 
      else 
       return false; 
     } 
    } 

    /// 

    /// Returns whether or not the firewall allows Application "Exceptions". 
    /// If the firewall is not installed, this returns false. 
    /// 

    /// 
    /// Added to allow access to this metho 
    /// 
    public bool AppAuthorizationsAllowed 
    { 
     get 
     { 
      if (IsFirewallInstalled && !fwMgr.LocalPolicy.CurrentProfile.ExceptionsNotAllowed) 
       return true; 
      else 
       return false; 
     } 
    } 

    /// 

    /// Adds an application to the list of authorized applications. 
    /// If the application is already authorized, does nothing. 
    /// 

    /// 
    ///   The full path to the application executable. This cannot 
    ///   be blank, and cannot be a relative path. 
    /// 
    /// 
    ///   This is the name of the application, purely for display 
    ///   puposes in the Microsoft Security Center. 
    /// 
    /// 
    ///   When applicationFullPath is null OR 
    ///   When appName is null. 
    /// 
    /// 
    ///   When applicationFullPath is blank OR 
    ///   When appName is blank OR 
    ///   applicationFullPath contains invalid path characters OR 
    ///   applicationFullPath is not an absolute path 
    /// 
    /// 
    ///   If the firewall is not installed OR 
    ///   If the firewall does not allow specific application 'exceptions' OR 
    ///   Due to an exception in COM this method could not create the 
    ///   necessary COM types 
    /// 
    /// 
    ///   If no file exists at the given applicationFullPath 
    /// 
    public void GrantAuthorization(string applicationFullPath, string appName) 
    { 
     #region Parameter checking 
     if (applicationFullPath == null) 
      throw new ArgumentNullException("applicationFullPath"); 
     if (appName == null) 
      throw new ArgumentNullException("appName"); 
     if (applicationFullPath.Trim().Length == 0) 
      throw new ArgumentException("applicationFullPath must not be blank"); 
     if (applicationFullPath.Trim().Length == 0) 
      throw new ArgumentException("appName must not be blank"); 
     if (applicationFullPath.IndexOfAny(Path.InvalidPathChars) >= 0) 
      throw new ArgumentException("applicationFullPath must not contain invalid path characters"); 
     if (!Path.IsPathRooted(applicationFullPath)) 
      throw new ArgumentException("applicationFullPath is not an absolute path"); 
     if (!File.Exists(applicationFullPath)) 
      throw new FileNotFoundException("File does not exist", applicationFullPath); 
     // State checking 
     if (!IsFirewallInstalled) 
      throw new FirewallHelperException("Cannot grant authorization: Firewall is not installed."); 
     if (!AppAuthorizationsAllowed) 
      throw new FirewallHelperException("Application exemptions are not allowed."); 
     #endregion 

     if (!HasAuthorization(applicationFullPath)) 
     { 
      // Get the type of HNetCfg.FwMgr, or null if an error occurred 
      Type authAppType = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication", false); 

      // Assume failed. 
      INetFwAuthorizedApplication appInfo = null; 

      if (authAppType != null) 
      { 
       try 
       { 
        appInfo = (INetFwAuthorizedApplication)Activator.CreateInstance(authAppType); 
       } 
       // In all other circumnstances, appInfo is null. 
       catch (ArgumentException) { } 
       catch (NotSupportedException) { } 
       catch (System.Reflection.TargetInvocationException) { } 
       catch (MissingMethodException) { } 
       catch (MethodAccessException) { } 
       catch (MemberAccessException) { } 
       catch (InvalidComObjectException) { } 
       catch (COMException) { } 
       catch (TypeLoadException) { } 
      } 

      if (appInfo == null) 
       throw new FirewallHelperException("Could not grant authorization: can't create INetFwAuthorizedApplication instance."); 

      appInfo.Name = appName; 
      appInfo.ProcessImageFileName = applicationFullPath; 
      // ... 
      // Use defaults for other properties of the AuthorizedApplication COM object 

      // Authorize this application 
      fwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(appInfo); 
     } 
     // otherwise it already has authorization so do nothing 
    } 
    /// 

    /// Removes an application to the list of authorized applications. 
    /// Note that the specified application must exist or a FileNotFound 
    /// exception will be thrown. 
    /// If the specified application exists but does not current have 
    /// authorization, this method will do nothing. 
    /// 

    /// 
    ///   The full path to the application executable. This cannot 
    ///   be blank, and cannot be a relative path. 
    /// 
    /// 
    ///   When applicationFullPath is null 
    /// 
    /// 
    ///   When applicationFullPath is blank OR 
    ///   applicationFullPath contains invalid path characters OR 
    ///   applicationFullPath is not an absolute path 
    /// 
    /// 
    ///   If the firewall is not installed. 
    /// 
    /// 
    ///   If the specified application does not exist. 
    /// 
    public void RemoveAuthorization(string applicationFullPath) 
    { 

     #region Parameter checking 
     if (applicationFullPath == null) 
      throw new ArgumentNullException("applicationFullPath"); 
     if (applicationFullPath.Trim().Length == 0) 
      throw new ArgumentException("applicationFullPath must not be blank"); 
     if (applicationFullPath.IndexOfAny(Path.InvalidPathChars) >= 0) 
      throw new ArgumentException("applicationFullPath must not contain invalid path characters"); 
     if (!Path.IsPathRooted(applicationFullPath)) 
      throw new ArgumentException("applicationFullPath is not an absolute path"); 
     if (!File.Exists(applicationFullPath)) 
      throw new FileNotFoundException("File does not exist", applicationFullPath); 
     // State checking 
     if (!IsFirewallInstalled) 
      throw new FirewallHelperException("Cannot remove authorization: Firewall is not installed."); 
     #endregion 

     if (HasAuthorization(applicationFullPath)) 
     { 
      // Remove Authorization for this application 
      fwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Remove(applicationFullPath); 
     } 
     // otherwise it does not have authorization so do nothing 
    } 
    /// 

    /// Returns whether an application is in the list of authorized applications. 
    /// Note if the file does not exist, this throws a FileNotFound exception. 
    /// 

    /// 
    ///   The full path to the application executable. This cannot 
    ///   be blank, and cannot be a relative path. 
    /// 
    /// 
    ///   The full path to the application executable. This cannot 
    ///   be blank, and cannot be a relative path. 
    /// 
    /// 
    ///   When applicationFullPath is null 
    /// 
    /// 
    ///   When applicationFullPath is blank OR 
    ///   applicationFullPath contains invalid path characters OR 
    ///   applicationFullPath is not an absolute path 
    /// 
    /// 
    ///   If the firewall is not installed. 
    /// 
    /// 
    ///   If the specified application does not exist. 
    /// 
    public bool HasAuthorization(string applicationFullPath) 
    { 
     #region Parameter checking 
     if (applicationFullPath == null) 
      throw new ArgumentNullException("applicationFullPath"); 
     if (applicationFullPath.Trim().Length == 0) 
      throw new ArgumentException("applicationFullPath must not be blank"); 
     if (applicationFullPath.IndexOfAny(Path.InvalidPathChars) >= 0) 
      throw new ArgumentException("applicationFullPath must not contain invalid path characters"); 
     if (!Path.IsPathRooted(applicationFullPath)) 
      throw new ArgumentException("applicationFullPath is not an absolute path"); 
     if (!File.Exists(applicationFullPath)) 
      throw new FileNotFoundException("File does not exist.", applicationFullPath); 
     // State checking 
     if (!IsFirewallInstalled) 
      throw new FirewallHelperException("Cannot remove authorization: Firewall is not installed."); 

     #endregion 

     // Locate Authorization for this application 
     foreach (string appName in GetAuthorizedAppPaths()) 
     { 
      // Paths on windows file systems are not case sensitive. 
      if (appName.ToLower() == applicationFullPath.ToLower()) 
       return true; 
     } 

     // Failed to locate the given app. 
     return false; 

    } 

    /// 

    /// Retrieves a collection of paths to applications that are authorized. 
    /// 

    /// 
    /// 
    ///   If the Firewall is not installed. 
    /// 
    public ICollection GetAuthorizedAppPaths() 
    { 
     // State checking 
     if (!IsFirewallInstalled) 
      throw new FirewallHelperException("Cannot remove authorization: Firewall is not installed."); 

     ArrayList list = new ArrayList(); 
     // Collect the paths of all authorized applications 
     foreach (INetFwAuthorizedApplication app in fwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications) 
      list.Add(app.ProcessImageFileName); 

     return list; 
    } 
    #endregion 
} 

/// 

/// Describes a FirewallHelperException. 
/// 

/// 
/// 
/// 
public class FirewallHelperException : System.Exception 
{ 
    /// 

    /// Construct a new FirewallHelperException 
    /// 

    /// 
    public FirewallHelperException(string message) 
     : base(message) 
    { } 
} 

क्लिकऑन सैंडबॉक्स में कोई समस्या नहीं आई है।

+4

लिंक टूटा हुआ प्रतीत होता है। – JAG

+5

यह लिंक काम करता है: http://web.archive.org/web/20070707110141/http://www.dot.net.nz/Default.aspx?tabid=42&mid=404&ctl=Details&ItemID=8 – Paya

+3

यह लिंक आज काम करता है: http://replay.web.archive.org/20081014105153/http://www.dot.net.nz/Default.aspx?tabid=42&mid=404&ctl=Details&ItemID=8 –

18

अगर यह सबसे अच्छा तरीका है, लेकिन चल netsh काम करना चाहिए सुनिश्चित नहीं हैं:

netsh फ़ायरवॉल allowedprogram सी जोड़ें: \ MyApp \ MyApp.exe MyApp सक्षम

मुझे लगता है कि इस प्रशासक अनुमति की आवश्यकता नहीं है, हालांकि, स्पष्ट कारणों :)

संपादित करें के लिए: मुझे यह जानने के लिए क्लिकऑन के बारे में पर्याप्त जानकारी नहीं है कि आप इसके माध्यम से बाहरी कार्यक्रम चला सकते हैं या नहीं।

+0

मुझे व्यक्तिगत रूप से इस समाधान को पसंद है। विंडोज एपीआई के साथ घूमने की तुलना में बाहरी प्रक्रिया को शुरू करना बहुत आसान है, खासकर जब क्यूटी/सी ++ जैसी चीजों का उपयोग करते हैं। – jocull

+7

यह ओएस निर्भर है और Win7 (इसके बहिष्कृत) के साथ काम नहीं करता है। आपको कुछ और बदसूरत करने की ज़रूरत है: netsh advfirewall फ़ायरवॉल नियम नाम = "मैसेंजर को अनुमति दें" dir = प्रोग्राम में = "c: \ प्रोग्राम फ़ाइलें \ messenger \ msmsgs.exe" सुरक्षा = authnoencap action = –

+1

बस जोड़ने के लिए, पूर्णता के लिए, ClickOnce परिनियोजन आपके एप्लिकेशन को इंस्टॉल करने के बाद क्या कर सकता है सीमित नहीं करता है, हालांकि यह स्टार्टअप पर अपडेट की जांच करने का प्रयास कर सकता है। – Basic

11

फ़ायरवॉल से डेटा तक पहुंचना संभव है, निम्न लेख देखें।

असली सवाल ClickOnce सैंडबॉक्स पहुँच इस तरह की अनुमति देता है करता है? मेरा अनुमान होगा कि यह नहीं है। शायद आप एक webservice का उपयोग कर सकते हैं? (ClickOnce में डेटा का उपयोग तरीकों के बारे में अधिक जानकारी के लिए Accessing Local and Remote Data in ClickOnce Applications देखें)

+0

यदि आपको बनाम 2010 पर संदर्भ hnetcfg जोड़ने में कोई समस्या है, तो इस लिंक को जांचें http://connect.microsoft.com/VisualStudio/feedback/details/575401/interop-netfwtypelib-dll-change – Alexandre

+0

मुझे एक्सेस अस्वीकृत अपवाद मिलता है जो मैं यहां उल्लेख किया है http://stackoverflow.com/questions/8605710/get-my-plication-to-be-allowed-access-through-firewall-using-c-sharp – PUG

3

सबसे आसान तरीका मैं netsh उपयोग करने के लिए किया जाएगा पता है, तो आप बस नियम को हटा सकते हैं और इसे फिर से बनाने के लिए, या यदि तुम्हारा, एक बंदरगाह नियम सेट निश्चित है।
Here एक पृष्ठ है जो इसके फ़ायरवॉल संदर्भ के विकल्पों का वर्णन करता है।

+0

** नेट्स ** ओएस-विशिष्ट है। आप सर्वर 2003 और सर्वर 2008 दोनों पर 1 नेटस् कमांड का उपयोग नहीं कर सकते हैं। – Achilles

1

जवाब आप केवल विश्वसनीय व्यवस्थापक के साथ चलाने के लिए सॉफ्टवेयर की अनुमति है विशेषाधिकार। समय-समय पर SOME सॉफ़्टवेयर में व्यवस्थापकीय विशेषाधिकार होना चाहिए और आपके सिस्टम में संवेदनशील परिवर्तन करना पड़ता है। आप शायद पढ़ने के लिए केवल हार्ड डिस्क पढ़ सकते हैं ...

+0

फ़ायरवॉल अपवाद जोड़ने के लिए ऊंचे विशेषाधिकारों की आवश्यकता नहीं है। – Bob77

+1

नेट्स एडफाइलवाल फ़ायरवॉल नियम नाम जोड़ें = "मेरा आवेदन" dir = action = कार्यक्रम की अनुमति दें = "सी: \ MyApp \ My App.exe" enable = yes अनुरोधित ऑपरेशन को उन्नयन की आवश्यकता है <व्यवस्थापक के रूप में चलाएं> –

1

यह उत्तर देर से हो सकता है।आप एक विधानसभा स्थापित किया जा रहा है के अंदर इस तरह एक इंस्टॉलर वर्ग की जरूरत है, और उसके बाद सुनिश्चित करें कि आप बनाना -

http://support.microsoft.com/kb/947709

0

मान लिया जाये कि हम एक दृश्य स्टूडियो Installer-> सेटअप परियोजना का उपयोग कर रहे: यह है कि मैं क्या का उपयोग कर समाप्त हो गया है इंस्टॉल चरण में "प्राथमिक आउटपुट" के लिए एक कस्टम कार्रवाई जोड़ें।

using System.Collections; 
using System.ComponentModel; 
using System.Configuration.Install; 
using System.IO; 
using System.Diagnostics; 

namespace YourNamespace 
{ 
    [RunInstaller(true)] 
    public class AddFirewallExceptionInstaller : Installer 
    { 
     protected override void OnAfterInstall(IDictionary savedState) 
     { 
      base.OnAfterInstall(savedState); 

      var path = Path.GetDirectoryName(Context.Parameters["assemblypath"]); 
      OpenFirewallForProgram(Path.Combine(path, "YourExe.exe"), 
            "Your program name for display"); 
     } 

     private static void OpenFirewallForProgram(string exeFileName, string displayName) 
     { 
      var proc = Process.Start(
       new ProcessStartInfo 
        { 
         FileName = "netsh", 
         Arguments = 
          string.Format(
           "firewall add allowedprogram program=\"{0}\" name=\"{1}\" profile=\"ALL\"", 
           exeFileName, displayName), 
         WindowStyle = ProcessWindowStyle.Hidden 
        }); 
      proc.WaitForExit(); 
     } 
    } 
} 
+0

यह मेरे लिए काम करता है –

+0

महत्वपूर्ण: ** "नेटस् फ़ायरवॉल" बहिष्कृत है; ** "नेटेश एडफाइलवॉल फ़ायरवॉल "इसके बजाय " नेट्स एडफाइलवॉल फ़ायरवॉल "का उपयोग करने के बारे में अधिक जानकारी के लिए"नेटस् फ़ायरवॉल" के बजाय, केबी आलेख 94770 9 [लिंक] पर देखें (https://go.microsoft.com/fwlink/?linkid=121488)। – antonio

संबंधित मुद्दे