6

I want Unique Device Id for back_end service (ws) for that I found following reference

  private string GetDeviceId()
    {
        var token = Windows.System.Profile.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("-", "");
    }//Note: This function may throw an exception. 

but, I can't understand the code , every time I get same Id for my Device (64 character string ), I want to know that it is applicable or not ? I couldn't found any reference from MSDN also

Thank you

Kushal Maniyar
  • 846
  • 1
  • 8
  • 26

1 Answers1

2

This may help:

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

    HashAlgorithmProvider hasher = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
    IBuffer hashed = hasher.HashData(hardwareId);

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

For the documentation, have a look at the GetPackageSpecificToken method in the HardwareIdentification class.

Stuart
  • 754
  • 11
  • 25
  • Instead of the hardcoded *"MD5"* you can use `HashAlgorithmNames.Md5` – Kristian Vukusic Aug 21 '15 at 14:20
  • @KristianVukusic Thank you. I have updated my answer. – Stuart Aug 21 '15 at 14:47
  • This PackageSpecificToken is changed once you change hardware profile (unplug BT or something else). I'd not rely on it. Maybe only for ad purposes. – Tertium Jun 02 '16 at 18:44
  • Anyway if you use it you should handle its "drift": https://msdn.microsoft.com/en-US/library/windows/apps/jj553431.aspx?f=255&MSPPError=-2147217396 – Tertium Jun 02 '16 at 19:35