21

This is my old implementation to get a Unique DeviceID for Windows Universal 8.1 but the type HardwareIdentification does not exist anymore.

    private static string GetId()
    {
        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("-", "");
    }
Jens Marchewka
  • 1,372
  • 1
  • 13
  • 22
  • FYI there's an even better way in the [Anniversary Update](http://stackoverflow.com/questions/40387615/how-to-get-unique-device-ids-on-windows-10-anniversary-update/40387616#40387616) – Peter Torr - MSFT Nov 02 '16 at 18:52
  • Is it possible to the see this same Id from windows, may be a commmand line or in the settings.?? I mean where does this hardware Id actually shows up. – Jins Peter Oct 24 '17 at 14:01

5 Answers5

19

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!");
        }
    }
}
Jens Marchewka
  • 1,372
  • 1
  • 13
  • 22
  • Any idea if... If you were to build an app or library targeting UWP used on both phone and desktop - can you use both phone and desktop SDKs or does it cause any problems with build, runtime or certification? – Filip Skakun Aug 16 '15 at 05:34
  • Hardware token can change if you add RAM to your PC, right ? – Grigory Nov 17 '15 at 19:42
  • @Grigory I believe so, I recall I saw something on msdn like cpu id and Ram size are taken into account when generating the token. – terry Dec 28 '15 at 22:46
  • 1
    @El I am getting different hardware id for same device – Kinjan Bhavsar Sep 15 '16 at 13:55
  • @KinjanBhavsar for multiple apps thats an expected behaviour i suggest. What context are you talking about? – Jens Marchewka Sep 26 '16 at 11:48
  • @JensMarchewka I tried this code on Windows 10 IoT Core [Version 10.0.15063] (Raspberry PI 3) but the result of GetId() changes every time the device restarts. Do you have any idea? – Claudio Cayo Castagnetti Jul 25 '17 at 15:05
  • Please see [this newer Q & A](https://stackoverflow.com/questions/40387615/how-to-get-unique-device-ids-on-windows-10-anniversary-update) for a much better way to get an ID in more recent Windows builds. – Peter Torr - MSFT Nov 04 '17 at 00:44
  • @JensMarchewka Is there a way to get the same Id from different apps.? DO u have any idea to make a corelation between the app and the DeviceID. so that I can use a central server for both app and know there two devices are same – Jins Peter Nov 14 '17 at 10:33
9

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?

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51
  • 2
    No I just need some sort of ID witch is unique for identify the device on APP abuse to lock some functions of the APP. Also to associate the push Uri within our DB for this user (chat). – Jens Marchewka Jul 31 '15 at 19:19
  • The device ID doesn't identify the user. It identifies the device. You need to implement another mechanism to generate and track user IDs (or rely on 3rd party login like Facebook or Microsoft Account) – Peter Torr - MSFT Jul 31 '15 at 19:39
  • 3
    That is exactly what we dont want. As you just can create new accounts to abuse our service again. – Jens Marchewka Aug 01 '15 at 08:29
  • 1
    What do you do when the customer sells the device to someone else? Now they are associated with the wrong account. – Peter Torr - MSFT Aug 01 '15 at 20:27
  • Why would they be associated wrong? After 2 Month the abuse lock will be restored. Also it is possible to have more than one device within the same Account. We need to replace the Push-URI at our Push Server if it changes, so how would you identify the URI to replace for device X? I don't know why we need to discuss this, with Android & iOS it works very well, why would we want to change a running and tested system? – Jens Marchewka Aug 03 '15 at 08:35
  • (1) Bob has a phone and abuses your app so you ban him. (2) Bob sells his phone to Alice. (3) Alice tries to use your app and is blocked for 2 months due to the ban on Bob's device. – Peter Torr - MSFT Aug 03 '15 at 15:13
  • Alice wont use the same Account. Alice is able to reach us on other ways to remove the lock earlier. But again, that's not the point. – Jens Marchewka Aug 04 '15 at 06:57
  • Also I am wondering, won't the DeviceId change if the device gets reset? If not, isn't it a big security problem? – Jens Marchewka Aug 04 '15 at 07:44
  • 1
    If the ID changed when the device was reset then your approach wouldn't work (Bob gets banned; Bob resets device; Bob is unbanned). – Peter Torr - MSFT Aug 04 '15 at 14:50
  • @PeterTorr-MSFT *You need to implement another mechanism to generate and track user IDs* - What's preferred? In my use case, I just want to keep track of API calls from installs of a consumer app, and make *reasonably* sure someone's not spamming/exploiting that cloud service. I could store a GUID in a local file and send it with each request, but would prefer a slightly obfuscated "real" hardware (or other built-in, unique) id so I'm not persisting info I don't need to. That doesn't call for a full-on Facebook/Google+/Passport ;) account login, I don't think. – ruffin Feb 16 '16 at 22:46
  • 1
    Hardware Token is OK for that as long as you have mitigations for when the device is given to another user. If you are only banning DoS-type attacks (within a short time window) it should be OK. – Peter Torr - MSFT Feb 17 '16 at 02:54
  • Is it possible to the see this same Id from windows, may be a commmand line or in the settings.?? I mean where does this hardware Id actually shows up.?? @PeterTorr-MSFT – Jins Peter Oct 24 '17 at 14:02
  • Is it possible to the see this same Id from windows, may be a commmand line or in the settings.?? I mean where does this hardware Id actually shows up. @JensMarchewka – Jins Peter Oct 24 '17 at 14:03
  • No, because it is specific to an app - you could build an app that would show you *its* ASHWID, but that would be different than any other app's ASHWID. – Peter Torr - MSFT Oct 24 '17 at 21:37
  • I just thank to god that this windows phone thing is dead now.. the restrictions for developers have finally besiege the verdict – Jens Marchewka Jul 12 '18 at 18:13
  • This API is not specific to Windows Phone. – Peter Torr - MSFT Jul 13 '18 at 00:34
8

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.

Jens Marchewka
  • 1,372
  • 1
  • 13
  • 22
1

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.

tobjak75
  • 23
  • 5
  • Do we have solution for this? EasClientDeviceInformation gives the same id for all phones – Neena Jul 03 '17 at 07:28
1
//you can use this
//its working with me very fine on windows 10
//replace the word bios with any hardware name you want
//data also can be found with using windows application named (wbemtest) 

using System.Management;
public static async Task<string> ReturnHardWareID()
        {
            string s = "";
            Task task = Task.Run(() =>
            {
                ManagementObjectSearcher bios = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS");
                ManagementObjectCollection bios_Collection = bios.Get();
                foreach (ManagementObject obj in bios_Collection)
                {
                    s = obj["SerialNumber"].ToString();
                    break; //break just to get the first found object data only
                }
            });
            Task.WaitAll(task);

            return await Task.FromResult(s);
        }
Hazem Elamir
  • 51
  • 1
  • 8