27

How to get the device unique id in Windows Phone 8.1? The old way of using DeviceExtendedProperties.GetValue("DeviceUniqueId") does not work for Windows Universal app.

Rhys
  • 4,411
  • 2
  • 21
  • 32
MohanRajNK
  • 865
  • 1
  • 13
  • 26
  • Any examples? I'm asking this question here, as well: http://stackoverflow.com/questions/36004003/windows-phone-device-unique-id – Glenn Strycker Jul 22 '16 at 16:01

2 Answers2

31
private string GetDeviceID()
{
    HardwareToken token = HardwareIdentification.GetPackageSpecificToken(null);
    IBuffer hardwareId = token.Id;

    HashAlgorithmProvider hasher = HashAlgorithmProvider.OpenAlgorithm("MD5");
    IBuffer hashed = hasher.HashData(hardwareId);

     string hashedString = CryptographicBuffer.EncodeToHexString(hashed);
     return hashedString;
}

Hope this help !

Robert MacLean
  • 38,077
  • 24
  • 96
  • 147
thongaduka
  • 617
  • 1
  • 8
  • 19
  • Why MD5 though? Why not any other algorithm?? – Apoorva Aug 07 '14 at 07:12
  • 9
    I would replace "MD5" to [HashAlgorithmNames.Md5](http://msdn.microsoft.com/en-us/library/windows.security.cryptography.core.hashalgorithmnames.md5.aspx). – Alexander.Ermolaev Oct 16 '14 at 12:24
  • You can replace CryptographicBuffer.EncodeToHexString with Convert.ToBase64String, it will produce shorter but still readable string. – Grigory Nov 05 '15 at 15:42
23

Note that when you write Universal App, it can be installed not only on phone. While on Phone technically hardware configuration is the same, on other devices it can change and so its ID. That's I think there is no such universal method to get ID. (more information you can find also here).

You may have a look at HardwareIdentification class and its method GetPackageSpecificToken:

HardwareToken myToken = HardwareIdentification.GetPackageSpecificToken(null);
IBuffer hardwareId = myToken.Id;

There is also a Guidance on using the App Specific Hardware ID (ASHWID) to implement per-device app logic.

Community
  • 1
  • 1
Romasz
  • 29,062
  • 12
  • 73
  • 140