0

I see the following code everywhere on StackOverflow. Supposedly it gives you access to the Windows Firewall API and lets you view and set firewall rules, etc:

var firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));

For example:

Is there any .NET API to get all the firewall rules

Can I get the firewall's status by c# code?

However, the type INetFwPolicy2 is not recognized by my ASP.NET 6 application, and I haven't been able to find what namespace it would be in.

Is the Windows Firewall API still available in ASP.NET 6? If not, how can I go about viewing Windows Firewall rules? This goes for ASP.NET Core 3.1 and 5 as well.

user3163495
  • 2,425
  • 2
  • 26
  • 43
  • It isn't in any (standard) namespace; that's an interface that would be made available through a COM interop library, since `HNetCfg.FwPolicy2` is a COM class. This is not a .NET-specific API. .NET Core 6 (or more accurately .NET Core 3+) does have support for COM interop, but I have no idea how far along that implementation is or how it's properly used. – Jeroen Mostert Feb 18 '22 at 20:59
  • 3
    This is tagged ASP.Net. Typically, an ASP.Net application should not run with permissions that would allow firewall rules to be queried. – Eric J. Feb 18 '22 at 21:00
  • 1
    There's also a `WindowsFirewallHelper` nuget available. – PMF Feb 18 '22 at 21:01
  • 2
    But I agree to @EricJ. A web application that sets firewall rules sounds like a major security risk. – PMF Feb 18 '22 at 21:02

0 Answers0