MatthewKing / DeviceId

A simple library providing functionality to generate a 'device ID' that can be used to uniquely identify a computer.
MIT License
729 stars 118 forks source link

DeviceId changed every time after reboot #7

Closed WangJi closed 5 years ago

WangJi commented 5 years ago

I'm using new DeviceIdBuilder().AddMacAddress().AddMotherboardSerialNumber().ToString() to identify a unique computer.
But it always changing after a reboot. Am I using it right ?

MatthewKing commented 5 years ago

Hello,

The motherboard serial number is derived from the Win32_BaseBoard WMI class. The serial number is assigned by the manufacturer, and DeviceId is just returning that value. Likewise, the MAC address identifier is built from the Win32_NetworkAdapterConfiguration WMI class, and DeviceID is just returning those values. It really depends on your OS and your underlying hardware as to whether these values are consistent. In an ideal world they SHOULD be, but in reality that's not guaranteed in every case.

AddMacAddress is probably the culprit here, as it's using all MAC addresses, even virtual ones. It may be worthwhile using a different component - maybe AddSystemDriveSerialNumber instead?

Hope this helps.

WangJi commented 5 years ago

Thanks for explanation, Matthew.
It is indeed Win32_NetworkAdapterConfiguration.MACAddress that returns different value each time.
Replacing AddMacAddress with AddSystemDriveSerialNumber did solve the problem.

delasource commented 4 years ago

I experience similar issues. Not every reboot and not every client. It seems to appear in a random case and only in virtual machines. Do you have a hint, which of these settings could be variable in my case?

new DeviceIdBuilder()
    .AddMachineName()
    .AddProcessorId()
    .AddMotherboardSerialNumber()
    .ToString();
MatthewKing commented 4 years ago

It would be either AddProcessorId or AddMotherboardSerialNumber, but it's hard to say exactly because it depends on how your VM is set up and what your VM is doing. I would recommend experimenting with different identifiers when running in a VM.

It may be possible to alter how AddProcessorId and AddMotherboardSerialNumber work within VMs but I don't know enough about all the different VM architectures and configurations to do so in a manner that will always be correct - so pull requests are welcome.