What is a good unique PC identifier?

后端 未结 13 1990
深忆病人
深忆病人 2020-11-27 10:59

I\'ve been looking at the code in this tutorial, and I found that it uses My.Computer.Name to save settings that shouldn\'t roam between computers. It\'s entire

相关标签:
13条回答
  • 2020-11-27 11:39

    If you are on windows HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\ CurrentVersion\ProductId is unique per machine/per windows install. where as in some of the other answers like the MAC address, Proc SN, and HD SN will stay the same between windows reinstalls/dual boot situations.

    0 讨论(0)
  • 2020-11-27 11:39

    In a managed network environment, the best, most reliable identifier might be the one you create, but there are some downsides.

    Some (many?) manufacturers provide a utility that allows you to set an asset tag that is stored in the firmware. This might be a bootable utility, or it might run within Windows, or it might even be built into the firmware setup. This "tag" is an arbitrary text string that you can set to whatever you want, and then read it back using WMI and the Win32_SystemEnclosure class...

    string[] selectedProperties = new string[] { "SMBIOSAssetTag" };
    ObjectQuery enclosureQuery = new SelectQuery("Win32_SystemEnclosure", null, selectedProperties);
    
    using (ManagementObjectSearcher enclosureSearcher = new ManagementObjectSearcher(enclosureQuery))
    using (ManagementObjectCollection enclosureCollection = enclosureSearcher.Get())
    {
        foreach (ManagementObject enclosure in enclosureCollection)
        {
            string assetTag = (string) enclosure.GetPropertyValue("SMBIOSAssetTag");
        }
    }
    

    Pros:

    • You can use whatever scheme you want (e.g. incorporating date, department, incrementing integers, GUIDs, etc.).
    • You can use one scheme for all machines regardless of their manufacturer, instead of having to handle manufacturer-specific schemes.
    • By allocating and tracking the identifiers yourself, you can guarantee that they are unique. Not relying on an identifier set by the manufacturer means there is no risk of duplicates within a manufacturer or between manufacturers.
    • The identifier is stored in the firmware — not on the hard drive — so it will survive reformatting, upgrades, etc. but also not be duplicated by backups/imaging/cloning.

    Cons:

    • You need to actually set the asset tag; they'll all be blank until you do so.
    • Setting a machine's asset tag may require physical access and a reboot.
    • Asset tags are not write-once and could, therefore, be changed or erased.
      • Password-protected firmware should require that password before changing the tag, but that's not guaranteed.
    • By allocating and tracking the identifiers yourself, there's not only the overhead of...allocating and tracking the identifiers, but also the possibility that you'll introduce duplicates if you're not careful.
    • Using asset tags for this purpose requires that all machines support setting an asset tag and properly report it to WMI.
    0 讨论(0)
  • 2020-11-27 11:41

    One thing you can use is the MAC of any Network interface. You can also combine several sources of information. Like HDD Serial number, mac, processor type to calculate a hash from it.

    0 讨论(0)
  • 2020-11-27 11:42

    Some good identifiers:

    • MAC Address: It's fairly easy to get at, and it's usually unique. However, it can be spoofed/changed rather easily, so it depends on how unique it needs to be.
    • CPU Serial Number: It's not available on lots of older systems, but it's there. Check out this MSDN page. It won't change, but it's bound to a computer.
    • HDD Serial Number: It's likely to not change, but can be a nuisance if the HD fails. Check out this MSDN page.
    0 讨论(0)
  • 2020-11-27 11:44

    Use the network card's MAC address. It's supposed to be unique. It can be changed, though. It depends on how malicious you expect your users to be and how critical your application is.

    Some sample code to do it:

    public string GetMACAddress() {
        ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection moc = mc.GetInstances();
    
        string MACAddress = String.Empty;
    
        foreach (ManagementObject mo in moc) {
            if (MACAddress == String.Empty) { // only return MAC Address from first card
                if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
            }
            mo.Dispose();
        }
    
        return MACAddress;
    }
    
    0 讨论(0)
  • 2020-11-27 11:44

    Each computer has a SID that's unique under normal circumstances.

    0 讨论(0)
提交回复
热议问题