How do I get a Unique Identifier for a Device within Windows 10 Universal?
That is the complete solution for Windows Desktop:
- Add the Extension reference "Windows Desktop Extensions for the UWP" like Peter Torr - MSFT mentioned.
Use this Code to get the HardwareId:
using System;
using Windows.Security.ExchangeActiveSyncProvisioning;
using Windows.System.Profile;
namespace Tobit.Software.Device
{
public sealed class DeviceInfo
{
private static DeviceInfo _Instance;
public static DeviceInfo Instance
{
get {
if (_Instance == null)
_Instance = new DeviceInfo();
return _Instance; }
}
public string Id { get; private set; }
public string Model { get; private set; }
public string Manufracturer { get; private set; }
public string Name { get; private set; }
public static string OSName { get; set; }
private DeviceInfo()
{
Id = GetId();
var deviceInformation = new EasClientDeviceInformation();
Model = deviceInformation.SystemProductName;
Manufracturer = deviceInformation.SystemManufacturer;
Name = deviceInformation.FriendlyName;
OSName = deviceInformation.OperatingSystem;
}
private static string GetId()
{
if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.System.Profile.HardwareIdentification"))
{
var token = HardwareIdentification.GetPackageSpecificToken(null);
var hardwareId = token.Id;
var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);
byte[] bytes = new byte[hardwareId.Length];
dataReader.ReadBytes(bytes);
return BitConverter.ToString(bytes).Replace("-", "");
}
throw new Exception("NO API FOR DEVICE ID PRESENT!");
}
}
}
EasClientDeviceInformation does not work for Windows 10 mobile. The device id is just the same for every phone (all our win10m customers gets registered with the same GUID) We need the id for sending push messages to the right phone.
Update for Windows 1609 ("Anniversary Update")
See this Q&A for a much better way to get an ID.
Old info for older OS builds
You need to add a reference to the Desktop and / or Mobile SDKs to build against the Hardware Token. At runtime you should use the ApiInformation
type to query if the API is present before using it (other device families like Xbox don't have it).
That said, many times when people ask for the device ID that's not actually the best solution for their problem -- are you sure you need to identify the physical device across its entire lifespan, even if ownership changes?
It seems that
var deviceInformation = new EasClientDeviceInformation();
string Id = deviceInformation.Id.ToString();
is doing the magic, refering to EasClientDeviceInformation it provides a unique Id.
The Id property represents the DeviceId using the GUID truncated from the first 16 bytes of the SHA256 hash of the MachineID, User SID, and Package Family Name where the MachineID uses the SID of the local users group.
BUT it only works for Windows Store Apps... so there have to be another solution.