我知道stackoverflow中有很多类似的问题,如下所示:
......还有几十个,我研究过它们.
问题是一些已接受的答案已将MAC地址建议为唯一标识符,这完全不正确.其他一些答案建议使用各种组件的组合,这似乎更合乎逻辑.但是,在使用组合的情况下,应该考虑哪些组件自然不可能经常更换.几天前,我们为软件许可问题开发了一个密钥生成器,我们使用CPUID和MAC的组合来唯一地识别一台Windows PC,直到实际测试我们认为我们的方法已经足够好了.具有讽刺意味的是,当我们进行测试时,我们发现有三台计算机使用我们的密钥生
那么,真的有任何方法可以唯一地识别任何计算机吗?现在我们只需要让我们的密钥生成器在windows pc上运行.使用c#的某种方式(如果可能的话)会很好,因为我们的系统是在.net上开发的.
更新:
很抱歉造成一些混乱和明显错误的警报.我们在检索HW信息的方法中发现了一些不正确之处.主要是我想删除这个问题,因为现在我自己的困惑已经消失,我确实认为两个或更多组件的组合足以识别计算机.然而,然后我决定保留它,因为我认为我应该澄清导致问题的原因,因为同样的事情可能会伤害其他人.
这就是我们正在做的事情(不包括其他代码):
我们使用getManagementInfo函数来检索MAC和处理器ID
private String getManagementInfo(String StrKey_String, String strIndex)
{
String strHwInfo = null;
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + StrKey_String);
foreach (ManagementObject share in searcher.Get())
{
strHwInfo += share[strIndex];
}
}
catch (Exception ex)
{
// show some error message
}
return strHwInfo;
}
Run Code Online (Sandbox Code Playgroud)
然后在需要的地方我们使用该函数来检索MAC地址
string strMAC = getManagementInfo("Win32_NetworkAdapterConfiguration", "MacAddress");
Run Code Online (Sandbox Code Playgroud)
并检索ProcessorID
string strProcessorId = getManagementInfo("Win32_Processor", "ProcessorId");
Run Code Online (Sandbox Code Playgroud)
此时,strMAC …
我们使用以下代码检索Windows PC的活动MAC地址.
private static string macId()
{
return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled");
}
private static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
{
string result = "";
System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
System.Management.ManagementObjectCollection moc = mc.GetInstances();
foreach (System.Management.ManagementObject mo in moc)
{
if (mo[wmiMustBeTrue].ToString() == "True")
{
//Only get the first one
if (result == "")
{
try
{
result = mo[wmiProperty].ToString();
break;
}
catch
{
}
}
}
}
return result;
}
//Return a hardware identifier
private static string identifier(string …Run Code Online (Sandbox Code Playgroud) 我试图从计算机中获取唯一的标识符,并希望每次都可以可靠地返回相同的MAC地址.相信我,我有使用MAC地址的原因,并阅读了许多关于备用唯一ID方法的帖子(是的,如果他们没有任何网卡,我会考虑).
问题出在.NET中我无论如何都不知道特定的NetworkInterface是否是一个物理硬件网卡,例如"Nortel IPSECSHM Adapter - Packet Scheduler Miniport",它会在您连接到某些VPN或WiFi网络时添加.
我知道如何通过使用类似于此的代码获取Mac地址:
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
log.Debug("NIC " + nic.OperationalStatus + " " + nic.NetworkInterfaceType + " " + nic.Speed + " " + nic.GetPhysicalAddress() + " " + nic.Description);
}
Run Code Online (Sandbox Code Playgroud)
可以理解的是,没有100%的方法可以确保我获得内部网卡,但我想选择MAC地址来返回最不可能改变的给定机器.独立于诸如以下因素 - 无论是否连接到wifi ......通过某种类型的系绳连接进行连接......或者他们安装了一些新的vpn软件,增加了新的界面.
1)选择第一个"Up"界面.这在我的笔记本电脑上失败了,因为"Packet Miniport"总是在运行.此外,如果我将手机连接到笔记本电脑,这也会显示为第一张卡.
2)选择最合适的类型......这个失败b/c基本上所有东西都显示为"以太网",包括WiFi适配器和我的iPHone网络连接.
3)选择具有IP地址的NIC.由于以下几个原因导致失败:1)网卡可能未连接到LAN 2)有多个可能具有IP地址的网卡.
4)只需发送所有MAC地址......问题是列表会根据安装的软件而改变,并且很难进行比较.
5)以最快的速度选择mac地址.我想这可能是我最好的选择.我认为可以说最快的界面通常是最永久的.
或者,可能还有其他方法可以检测.NET中的物理卡,或者如果您可以推荐一个提供不同信息的API调用,我会考虑调用其他API调用.
还有其他想法吗?
这里演示的是当我的iphone被束缚时上面的示例代码的输出:
DEBUG - NIC Down Ethernet 500000 0021E98BFBEF Apple Mobile Device Ethernet - Packet Scheduler Miniport
DEBUG - NIC Up Ethernet 10000000 …Run Code Online (Sandbox Code Playgroud)