How to get a unique identifier for a machine running Windows 8 in C #?

I am working on a Metro application written in C # and you need a way to uniquely identify the device. I found ASHWID in the documentation, which looks great. The suggested code is as follows:

HardwareToken token = HardwareIdentification.GetPackageSpecificToken(null); IBuffer hardwareId = token.Id; IBuffer signature = token.Signature; IBuffer certificate = token.Certificate; 

The problem is, how do I turn this IBuffer into a string that I can use?

+7
source share
4 answers

After a big hunt with the suggestions that were actually in JS or C ++, I finally found the answer!

 private string GetHardwareId() { 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); } 

Thanks for this blog - http://bartwullems.blogspot.co.uk/2012/09/windows-8-uniquely-identifying-device.html

+12
source

This should work too, but I don't have Windows 8 to test with ...

 private string GetHardwareId() { return BitConverter.ToString(Windows.System.Profile.HardwareIdentification.GetPackageSpecificToken(null).Id.ToArray()); } 

And if you call it more than once, you might want to paste it into Lazy<T>

 private static Lazy<string> _hardwareId = new Lazy<string>(() => BitConverter.ToString(Windows.System.Profile.HardwareIdentification.GetPackageSpecificToken(null).Id.ToArray()), true); public string HardwareId() { return _hardwareId.Value; } 

Or just make it static if you know that it will always be called:

 public static readonly string HardwareId = BitConverter.ToString(Windows.System.Profile.HardwareIdentification.GetPackageSpecificToken(null).Id.ToArray())); 
+4
source

You can use HardwareIdentification.GetPackageSpecificToken(null) , see http://msdn.microsoft.com/en-us/library/windows/apps/jj553431.aspx

This function gives you a lot of information that you can filter as you wish. For example:

 public static string GetMachineId() { var hardwareToken = HardwareIdentification.GetPackageSpecificToken(null).Id.ToArray(); var count = hardwareToken.Length / 4; ulong id = 0ul; for (int i = 0; i < count; i++) { switch (BitConverter.ToUInt16(hardwareToken, i * 4)) { case 1: // processor case 2: // memory case 9: // system BIOS id = (id << 12) ^ BitConverter.ToUInt16(hardwareToken, i * 4 + 2); break; } } return Convert.ToBase64String(BitConverter.GetBytes(id)); } 

However, keep in mind that this feature and the underlying API cannot guarantee the absolute uniqueness of all machines connected to the Internet. Usually you combine this with user information.

Another option is to generate and store a GUID in local (non-roaming) storage and use it as an identifier for your computer. Depending on your specific needs, this may be the best solution.

+3
source

For guid, you can do the following as an extension for the specified answer

  private Guid GetHardwareId() { 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); byte[] deviceId = new byte[16]; Array.Copy((byte[])bytes, deviceId, deviceId.Length); return new Guid(deviceId); } 
0
source

All Articles