DeviceId
A simple library providing functionality to generate a 'device ID' that can be used to uniquely identify a computer.
Install / Use
/learn @MatthewKing/DeviceIdREADME
DeviceId
A simple library providing functionality to generate a 'device ID' that can be used to uniquely identify a computer.
Quickstart
What packages are needed?
As of version 6, the packages have been split up so that users can pick-and-choose what they need, without having to pull down unnecessary references that they won't use:
- The main DeviceId package contains the core functionality and a number of cross-platform components.
- The DeviceId.Windows package adds a few Windows-specific components.
- The DeviceId.Windows.Wmi package adds even more Windows-specific components, using WMI.
- The DeviceId.Windows.WmiLight package adds the same components as above, but using WmiLight instead of WMI.
- The DeviceId.Windows.Mmi package adds the same components as above, but using MMI instead of WMI for those instances where WMI isn't appropriate (such as where no .NET Framework is present on the machine).
- The DeviceId.Linux package adds a few Linux-specific components.
- The DeviceId.Mac package adds a few Mac-specific components.
- The DeviceId.SqlServer package adds support for generating a database ID for SQL Server databases.
You can pick-and-choose which packages to use based on your use case.
For a standard Windows app, the recommended packages are: DeviceId and DeviceId.Windows. If you want some extra advanced components you can also add DeviceId.Windows.Wmi.
PM> Install-Package DeviceId
PM> Install-Package DeviceId.Windows
Building a device identifier
Use the DeviceIdBuilder class to build up a device ID.
Here's a Windows-specific device ID, using the DeviceId.Windows package to get the built-in Windows Device ID.
string deviceId = new DeviceIdBuilder()
.OnWindows(windows => windows.AddWindowsDeviceId())
.ToString();
Here's a simple cross-platform one, using only the DeviceId package, which is valid for both version 5 and version 6 of the library:
string deviceId = new DeviceIdBuilder()
.AddMachineName()
.AddOsVersion()
.AddFileToken("example-device-token.txt")
.ToString();
Here's a more complex device ID, making use of some of the advanced components from the DeviceId.Windows.Wmi (or DeviceId.Windows.Mmi) package:
string deviceId = new DeviceIdBuilder()
.AddMachineName()
.AddOsVersion()
.OnWindows(windows => windows
.AddProcessorId()
.AddMotherboardSerialNumber()
.AddSystemDriveSerialNumber())
.ToString();
Here's a complex cross-platform device ID, using DeviceId.Windows.Wmi, DeviceId.Linux, and DeviceId.Mac:
string deviceId = new DeviceIdBuilder()
.AddMachineName()
.AddOsVersion()
.OnWindows(windows => windows
.AddProcessorId()
.AddMotherboardSerialNumber()
.AddSystemDriveSerialNumber())
.OnLinux(linux => linux
.AddMotherboardSerialNumber()
.AddSystemDriveSerialNumber())
.OnMac(mac => mac
.AddSystemDriveVolumeUUID()
.AddPlatformSerialNumber())
.ToString();
You can also generate a unique identifier for a database instance. Currently, only SQL Server is supported, but more may be added if there is demand and/or community support:
using SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
string databaseId = new DeviceIdBuilder()
.AddSqlServer(connection, sql => sql
.AddServerName()
.AddDatabaseName()
.AddDatabaseId())
.ToString();
What can you include in a device identifier
The following extension methods are available out of the box to suit some common use cases:
From DeviceId:
AddUserNameadds the current user's username to the device identifer.AddMachineNameadds the machine name to the device identifier.AddOsVersionadds the current OS version to the device identifier. Note: This usesEnvironment.OSVersion, so if you're targeting older .NET Framework versions, you'll get different values compared to when you target more modern versions of .NET (.NET Core, .NET 5, .NET 6, and anything later than that).AddMacAddressadds the MAC address to the device identifier.AddFileTokenadds a unique token stored in a file to the device identifier. The file is created if it doesn't already exist. Fails silently if no permissions available to access the file.
From DeviceId.Windows:
AddWindowsDeviceIdadds the Windows Device ID (also known as Machine ID or Advertising ID) to the device identifier. This value is the one displayed as "Device ID" in the Windows Device Specifications UI.AddWindowsProductIdadds the Windows Product ID to the device identifier. This value is the one displayed as "Product ID" in the Windows Device Specifications UI.AddRegistryValueadds a specified registry value to the device identifier.AddMachineGuidadds the machine GUID fromHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptographyto the device identifier.
From DeviceId.Windows.Wmi / DeviceId.Windows.WmiLight / DeviceId.Windows.Mmi:
AddMacAddressFromWmi/AddMacAddressFromMmiadds the MAC address to the device identifier. These use the improved query functionality from WMI/MMI to provide additional functionality over the basicAddMacAddressmethod (such as being able to exclude non-physical device).AddProcessorIdadds the processor ID to the device identifier. On ARM64 systems where ProcessorId is not available, it automatically falls back to a combination of Manufacturer, Name, and NumberOfCores.AddSystemDriveSerialNumberadds the system drive's serial number to the device identifier.AddMotherboardSerialNumberadds the motherboard serial number to the device identifier.AddSystemUuidadds the system UUID to the device identifier.
From DeviceId.Linux:
AddSystemDriveSerialNumberadds the system drive's serial number to the device identifier.AddMotherboardSerialNumberadds the motherboard serial number to the device identifier. On ARM systems where DMI is not available, it falls back to the Device Tree model.AddMachineIdadds the machine ID (from/var/lib/dbus/machine-idor/etc/machine-id) to the device identifier.AddProductUuidadds the product UUID (from/sys/class/dmi/id/product_uuid) to the device identifier. On ARM systems where DMI is not available, it falls back to the Device Tree serial number.AddCpuInfoadds CPU info (from/proc/cpuinfo) to the device identifier.AddDockerContainerIdadds the Docker container identifier (from/proc/1/cgroup) to the device identifier.AddDeviceTreeSerialNumberadds the Device Tree serial number to the device identifier. This is typically available on ARM-based Linux systems.AddDeviceTreeModeladds the Device Tree model to the device identifier. This is typically available on ARM-based Linux systems.
From DeviceId.Mac:
AddSystemDriveVolumeUUIDadds the system drive's Volume UUID to the device identifier.AddPlatformSerialNumberadds IOPlatformSerialNumber to the device identifier.
From DeviceId.SqlServer:
AddServerNameadds the server name to the device identifier.AddDatabaseNameadds the server name to the device identifier.AddDatabaseIdadds the database ID to the device identifier.AddServerPropertyadds a specified server property to the device identifier.AddServerPropertyadds a specified extended property to the device identifier.
Dealing with MAC Address randomization and virtual network adapters
Non physical network adapters like VPN connections tend not to have fixed MAC addresses. For wireless (802.11 based) adapters hardware (MAC) address randomization is frequently applied to avoid tracking with many modern operating systems support this out of the box. This makes wireless network adapters bad candidates for device identification.
Using the cross-platform AddMacAddress, you can exclude wireless network adapters like so:
string deviceId = new DeviceIdBuilder()
.AddMacAddress(excludeWireless: true)
.ToString();
If you're on Windows, you can also exclude non-physical adapters using the DeviceId.Windows.Wmi or DeviceId.Windows.Mmi packages like so:
string deviceId = new DeviceIdBuilder()
.AddMacAddress(excludeWireless: true)
.OnWindows(windows => windows
.AddMacAddressFromWmi(excludeWireless: true, excludeNonPhysical: true)
.ToString()
Controlling how the device identifier is formatted
Use the UseFormatter method to set the formatter:
string deviceId = new DeviceIdBuilder()
.AddMachineName()
.AddOsVersion()
.UseFormatter(new HashDeviceIdFormatter(() => SHA256.Create(), new Base32ByteArrayEncoder()))
.ToString();
The "default" formatters are available in DeviceIdFormatters for quick reference. The default formatter changed between version 5 and version 6 of the library. If you're using version 6 but want to revert to the version 5 formatter, you can do so:
string deviceId = new DeviceIdBuilder()
.AddMachineName()
.AddOsVersion()
.UseFormatter(DeviceIdFormatters.DefaultV5)
.ToString();
For more advanced usage scenarios, you can use one of the out-of-the-box implementations of IDeviceIdFormatter in the DeviceId.Formatters namespace, or you can create your own.
- StringDeviceIdFormatter - Format
