/// <summary> /// Authorize an application to work through the firewall /// </summary> /// <param name="title">Name of Firewall Rule</param> /// <param name="applicationPath">Path to executable</param> /// <param name="scope">Scope (All, Local, Custom)</param> /// <param name="ipVersion">IPv4, IpV6 or both</param> /// <returns>True if it succeeds</returns> public static bool AuthorizeApplication(string title, string applicationPath, NET_FW_SCOPE scope, NET_FW_ACTION action, NET_FW_IP_VERSION ipVersion) { try { if (OSVersion.GetOSVersion() <= OSVersion.OS.WIN_2003) { dynamic fwMgr = CreateCOMObject(PROGID_FIREWALL_MANAGER); dynamic profile = fwMgr.LocalPolicy.CurrentProfile; dynamic authApp = CreateCOMObject(PROGID_AUTHORIZED_APPLICATION); authApp.Name = title; authApp.ProcessImageFileName = applicationPath; authApp.Scope = scope; authApp.IpVersion = ipVersion; authApp.Enabled = true; profile.AuthorizedApplications.Add(authApp); } else { dynamic firewallRule = CreateCOMObject(PROGID_FW_RULE); firewallRule.Action = action; firewallRule.Name = title; firewallRule.ApplicationName = applicationPath; firewallRule.Enabled = true; firewallRule.InterfaceTypes = "All"; firewallRule.EdgeTraversal = true; dynamic firewallPolicy = CreateCOMObject(PROGID_FW_POLICY); firewallPolicy.Rules.Add(firewallRule); } } catch (Exception e) { Log.WriteSystemEventLog("Error authorizing firewall application -> " + e.ToString(), System.Diagnostics.EventLogEntryType.Warning); return false; } return true; }
/// <summary> /// Authorize an application to work through the firewall /// </summary> /// <param name="title">Name of Firewall Rule</param> /// <param name="applicationPath">Path to executable</param> /// <param name="scope">Scope (All, Local, Custom)</param> /// <param name="ipVersion">IPv4, IpV6 or both</param> /// <returns>True if it succeeds</returns> public static bool AuthorizeApplication(string title, string applicationPath, NET_FW_SCOPE scope, NET_FW_ACTION action, NET_FW_IP_VERSION ipVersion) { try { if (OSVersion.GetOSVersion() <= OSVersion.OS.WIN_2003) { dynamic fwMgr = CreateCOMObject(PROGID_FIREWALL_MANAGER); dynamic profile = fwMgr.LocalPolicy.CurrentProfile; dynamic authApp = CreateCOMObject(PROGID_AUTHORIZED_APPLICATION); authApp.Name = title; authApp.ProcessImageFileName = applicationPath; authApp.Scope = scope; authApp.IpVersion = ipVersion; authApp.Enabled = true; profile.AuthorizedApplications.Add(authApp); } else { dynamic firewallRule = CreateCOMObject(PROGID_FW_RULE); firewallRule.Action = action; firewallRule.Name = title; firewallRule.ApplicationName = applicationPath; firewallRule.Enabled = true; firewallRule.InterfaceTypes = "All"; firewallRule.EdgeTraversal = true; dynamic firewallPolicy = CreateCOMObject(PROGID_FW_POLICY); firewallPolicy.Rules.Add(firewallRule); } } catch (Exception e) { Log.WriteSystemEventLog("Error authorizing firewall application -> " + e.ToString(), System.Diagnostics.EventLogEntryType.Warning); return(false); } return(true); }