简体   繁体   English

如何唯一地识别不是* VPN连接的网络连接?

[英]How do I uniquely identify only network connections that are *not* VPN connections?

I'm trying to track all current network connections at run time, but I want to exclude VPN connections. 我试图在运行时跟踪所有当前的网络连接,但我想排除VPN连接。

I need to be able to identify the network(s) being connected to, as well, so I'm using NetworkListManagerClass() to build a list of current connections: 我需要能够识别连接到的网络,所以我使用NetworkListManagerClass()来构建当前连接的列表:

var manager = new NetworkListManagerClass();
var connectedNetworks = manager.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_CONNECTED).Cast<INetwork>();

I believe I can determine if a connection adapter is for a VPN by examining the NetworkInterfaceType of the NetworkInterface class: 我相信我可以通过检查NetworkInterface类的NetworkInterfaceType来确定连接适配器是否适用于VPN:

NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces().Where(n => n.OperationalStatus == OperationalStatus.Up).ToArray();

      foreach (NetworkInterface n in interfaces)
      {
          if (n.NetworkInterfaceType == NetworkInterfaceType.Tunnel || n.NetworkInterfaceType == NetworkInterfaceType.PPP)
          {

          }
      }

However, it isn't enough for me to just know that an interface is or is not a VPN. 但是,仅仅知道接口是VPN还是不是VPN是不够的。 I need to be able to track details about the network that the non-VPN interfaces are connected to. 我需要能够跟踪非VPN接口所连接的网络的详细信息。

For example: 例如:

We'll call my work network "officenetwork". 我们称我的工作网络为“officenetwork”。 We'll call my home network "homenetwork". 我们称我的家庭网络为“家庭网络”。

If I'm at work, I can see I'm connected to officenetwork by examining NetworkListManagerClass based off the NLM_ENUM_NETWORK_CONNECTED status. 如果我在工作,我可以通过检查基于NLM_ENUM_NETWORK_CONNECTED状态的NetworkListManagerClass来看到我已连接到officenetwork My application should know that officenetwork is my current local network, and will store and reference settings specific to that network. 我的应用程序应该知道officenetwork是我当前的本地网络,并将存储和引用特定于该网络的设置。

Similarly, if I'm at home, I should be able to see homenetwork , and store and reference a different set of settings specific to that network. 同样,如果我在家,我应该能够看到家庭网络 ,并存储和引用一组不同的特定于该网络的设置。

However, if I'm at home, and I use a VPN to connect to officenetwork , examining NetworkListManagerClass based off the NLM_ENUM_NETWORK_CONNECTED status will show both homenetwork and officenetwork . 但是,如果我在家,并且使用VPN连接到officenetwork ,则根据NLM_ENUM_NETWORK_CONNECTED状态检查NetworkListManagerClass将显示homenetworkofficenetwork Since the connection to officenetwork is a VPN, I need to ignore it, and only store and reference the settings for homenetwork . 由于与officenetwork的连接是VPN,我需要忽略它,并且只存储和引用homenetwork的设置。

This software will be for users who will connect to networks I know nothing about, so I cannot assume connection data for specific VPN's or networks. 该软件适用于将连接到我一无所知的网络的用户,因此我无法假设特定VPN或网络的连接数据。

How can I check current network connections and identify only networks that are not VPN connections, so I can store custom settings for that network? 如何检查当前网络连接并仅识别不是 VPN连接的网络,以便我可以存储该网络的自定义设置?

From an INetwork instance, you can get the list of connections ( INetworkConnections instances) using the GetNetworkConnections method. INetwork实例中,您可以使用GetNetworkConnections方法获取连接列表( INetworkConnections实例)。

Then, using the INetworkConnection::GetAdapterId method , you can get a Guid that represent the adapter Id. 然后,使用INetworkConnection :: GetAdapterId方法 ,您可以获得表示适配器ID的Guid。 The adapter Id in fact matches the NetworkInterface.Id Property (it's a string, but you can convert it to a Guid). 实际上,适配器Id与NetworkInterface.Id属性匹配(它是一个字符串,但您可以将其转换为Guid)。

Here is a sample console app that dumps all network and their connections, and interface properties (type, name, etc.): 这是一个示例控制台应用程序,它转储所有网络及其连接和接口属性(类型,名称等):

class Program
{
    static void Main(string[] args)
    {
        var mgr = new NetworkListManager();
        foreach (INetwork network in mgr.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_ALL))
        {
            Console.WriteLine("Network: " + network.GetName());
            foreach (INetworkConnection conn in network.GetNetworkConnections())
            {
                Console.WriteLine(" Adapter Id:  " + conn.GetAdapterId());
                var ni = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(i => new Guid(i.Id) == conn.GetAdapterId());
                Console.WriteLine(" Interface: " + ni.Name);
                Console.WriteLine(" Type: " + ni.NetworkInterfaceType);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM