本文介绍了如何确定实际的物理网卡的MAC地址 - 通过VPN的(.NET C#)创建的不是虚拟的网络接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我试图让获得一个唯一的标识符了一台电脑,并希望能够可靠地返回相同的MAC地址各一次。相信我,我有我的理由使用的MAC地址,看了很多帖子关于其他唯一的ID的方法(是的,我一直认为,如果他们没有任何网络卡)。

问题

现在的问题是在.NET中我没有看到反正讲一个具体的NetworkInterface是否是有点像北电IPSECSHM适配器 - 数据包计划程序微型端口的物理硬件网络卡,当您连接到特定的VPN或得到补充WiFi网络。

我知道如何使用code与此类似,以获得MAC地址:

 的foreach(NetworkInterface的NIC在NetworkInterface.GetAllNetworkInterfaces())
    {
        log.Debug(NIC+ nic.OperationalStatus ++ nic.NetworkInterfaceType ++ nic.Speed​​ ++ nic.GetPhysicalAddress()++ nic.Description);
    }
 

可以理解的,没有100%的方式,以确保我会得到一个内部网卡,但我想去捡MAC地址返回这对于一个给定机,这是最有可能改变。独立的因素,如 - 无论是连接到wifi的......被通过某种类型的系绳连接连接...或者安装一些新的VPN软件增加了一个新的接口

策略考虑

1)选择所述第一接口,是「上」。这种失败对我的笔记本电脑,因为数据包微型端口是始终。此外,如果我系绳我的手机给我的笔记本电脑,这也显示为第一张牌。

2)选择最合适的类型。这失败B / C基本都显示为以太网包括WiFi适配器和我的iPhone网络共享互联网连接。

3)选择具有IP地址的网卡。失败有以下几个原因:1)网络卡可能无法连接到LAN 2)有可能有多个IP地址的网卡

4)只要发送所有MAC地址...问题是列表中会改变的基础上安装的软件,它会是很难比较。

5)选择以最快的速度MAC地址。我想,这大概是我最好的选择。我认为这是肯定地说,最快的接口通常将是最持久的。

另外,有可能检测到物理卡在.NET一些其他的方式,或我会考虑调用其他的API调用,如果你能推荐一个,将提供不同的信息。

任何其他的想法?

要在这里证明是我的样品code以上时,我有我的iPhone拴输出:

  DEBUG  - 网卡向下以太网500000 0021E98BFBEF苹果移动设备的以太网 - 数据包计划程序微型端口
DEBUG  - 网卡建立以太网千万444553544200北电IPSECSHM适配器 - 数据包计划程序微型端口
DEBUG  - 网卡向下以太网5400 00166FAC94C7英特尔(R)PRO /无线2200BG网络连接 - 数据包计划程序微型端口
DEBUG  - 网卡向下以太网十亿0016D326E957 Broadcom NetXtreme千兆以太网 - 数据包计划程序微型端口
DEBUG  -  NIC最多回环千万MS TCP环回接口
 

如果没有iPhone连接:

  DEBUG  -  NIC建立以太网千万444553544200北电IPSECSHM适配器 - 数据包计划程序微型端口
DEBUG  - 网卡向下以太网5400 00166FAC94C7英特尔(R)PRO /无线2200BG网络连接 - 数据包计划程序微型端口
DEBUG  - 网卡向下以太网十亿0016D326E957 Broadcom NetXtreme千兆以太网 - 数据包计划程序微型端口
DEBUG  -  NIC最多回环千万MS TCP环回接口
 

解决方案

这是我的方法:它使用物理卡被连接到PCI接口的事实

  ManagementObjectSearcher搜索=新ManagementObjectSearcher
    (选择MACADDRESS,PNPDeviceID从Win32_NetworkAdapter这WHERE MACADDRESS IS NOT NULL和PNPDeviceID IS NOT NULL);
ManagementObjectCollection mObject = searcher.Get();

的foreach(在mObject的ManagementObject OBJ)
{
    字符串PNP = OBJ [PNPDeviceID]的ToString()。
    如果(pnp.Contains(PCI \\))
    {
        字符串MAC = OBJ [MACADDRESS]的ToString()。
        MAC = mac.Replace(:,的String.Empty);
        返回MAC;
    }
}
 

Background

I'm trying to get obtain a unique identifier out of a computer and want to be able to reliably return the same MAC address each time. Trust me I have my reasons for using MAC address and have read many posts about alternate unique id methods (and yes i've considered if they don't have any network cards).

Problem

The problem is in .NET i don't see anyway to tell whether a specific NetworkInterface is a physical hardware network card from something like a "Nortel IPSECSHM Adapter - Packet Scheduler Miniport" which get added when you connect to certain VPNs or WiFi networks.

I know how to get the Mac Addresses by using code similar to this:

    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        log.Debug("NIC " + nic.OperationalStatus + " " + nic.NetworkInterfaceType + " " + nic.Speed + " " + nic.GetPhysicalAddress() + " " + nic.Description);
    }

Understandably there is no 100% way to make sure i'd be getting an internal network card but i'd like to pick the MAC address to return which for a given machine which is least likely to change. Independent of factors such as -- whether it's connected to wifi... gets connected via some type of tether connection... or they install some new vpn software which adds a new interface.

Strategies Considered

1) Choose the first interface that is "Up". This fails on my laptop because the "Packet Miniport" is always up. Additionally, if I tether my phone to my laptop this also shows up as the first card.

2) Choose the most appropriate type... This fails b/c basically everything shows up as "Ethernet" including WiFi Adapters and my iPHone tethering internet connection.

3) Pick the NIC which has an IP address. Fails for several reasons: 1) Network card might not be connected to LAN 2) There are multiple nics which might have IP Addresses.

4) Just send all MAC addresses... Problem is the list would change based on installed software and it'll be difficult to compare.

5) Pick the mac address with the fastest speed. I think this is probably my best bet. I think it's safe to say that the fastest interface is usually going to be the most permanent.

Alternatively, there may be some other way to detect physical cards in .NET or I'd consider invoking other API calls if you could recommend one that will provide different information.

Any other ideas?

To demonstrate here is the output of my sample code above when I have my iphone tethered:

DEBUG - NIC Down Ethernet 500000     0021E98BFBEF Apple Mobile Device Ethernet - Packet Scheduler Miniport
DEBUG - NIC Up   Ethernet 10000000   444553544200 Nortel IPSECSHM Adapter - Packet Scheduler Miniport
DEBUG - NIC Down Ethernet 54000000   00166FAC94C7 Intel(R) PRO/Wireless 2200BG Network Connection - Packet Scheduler Miniport
DEBUG - NIC Down Ethernet 1000000000 0016D326E957 Broadcom NetXtreme Gigabit Ethernet - Packet Scheduler Miniport
DEBUG - NIC Up   Loopback 10000000  MS TCP Loopback interface

Without Iphone Connected:

DEBUG - NIC Up   Ethernet 10000000   444553544200 Nortel IPSECSHM Adapter - Packet Scheduler Miniport
DEBUG - NIC Down Ethernet 54000000   00166FAC94C7 Intel(R) PRO/Wireless 2200BG Network Connection - Packet Scheduler Miniport
DEBUG - NIC Down Ethernet 1000000000 0016D326E957 Broadcom NetXtreme Gigabit Ethernet - Packet Scheduler Miniport
DEBUG - NIC Up   Loopback 10000000  MS TCP Loopback interface
解决方案

This is my method:it uses the fact that physical card is connected to PCI interface

ManagementObjectSearcher searcher = new ManagementObjectSearcher
    ("Select MACAddress,PNPDeviceID FROM Win32_NetworkAdapter WHERE MACAddress IS NOT NULL AND PNPDeviceID IS NOT NULL");
ManagementObjectCollection mObject = searcher.Get();

foreach (ManagementObject obj in mObject)
{
    string pnp = obj["PNPDeviceID"].ToString();
    if (pnp.Contains("PCI\\"))
    {
        string mac = obj["MACAddress"].ToString();
        mac = mac.Replace(":", string.Empty);
        return mac;
    }
}

这篇关于如何确定实际的物理网卡的MAC地址 - 通过VPN的(.NET C#)创建的不是虚拟的网络接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 16:48