且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Windows 10 周年更新上获取唯一的设备 ID

更新时间:2023-12-03 23:05:46

2017 年 11 月 3 日更新(新的 Registry 值,如下)

Windows 10 周年更新引入了新的 SystemIdentification type 这正是你想要的.与旧的 ASHWID 相比,它有几个优点:

The Windows 10 Anniversary Update introduced the new SystemIdentification type which does exactly what you want. It has several benefits over the old ASHWID:

  • 它适用于所有 Windows 10 平台
    • 注意:ASHWID 现在也可在所有平台上使用,但仍有上面列出的其他缺点
    • 注意:这对企业场景最有用;您不太可能在没有充分理由的情况下为 Windows 应用商店批准使用此功能的应用,因为它代表了隐私问题

    API 有一个小缺点:它不适用于某些旧 PC,因为它需要 UEFI 或 TPM.过去 5 年以上制造的大多数 PC 都应具有此硬件,并且所有其他非 PC 设备(电话、Xbox、HoloLens 等)都具有正确的硬件.如果您发现 PC 没有硬件,则需要回退到 ASHWID 或其他一些机制.

    There is one minor drawback to the API: it won't work on some old PCs, since it requires either UEFI or a TPM. Most PCs built in the last 5+ years should have this hardware, and all other non-PC devices (phone, Xbox, HoloLens, etc.) have the correct hardware. If you find a PC that doesn't have the hardware, you will need to fall back to the ASHWID or some other mechanism.

    从 Windows Fall Creator's Update(又名 1709 或 RS3 或通用 API 合同 5)开始,有 一个新的Registry标识源,它提供了一个相对稳定的ID,以防用户没有合适的硬件.如果用户重新安装操作系统(不是升级,而是新安装)或用户更改注册表,但其他方面与 Uefi 或 Tmp代码>.

    Starting with the Windows Fall Creator's Update (aka 1709 or RS3 or Universal API Contract 5) there is a new Registry identification source which provides a relatively stable ID in case the user doesn't have appropriate hardware. It will change if the user does a fresh re-install of the OS (not an upgrade, but a new install) or if the user changes the registry, but otherwise has the same benefits as Uefi or Tmp.

    使用 API 很简单;无需在后端进行复杂的解析或考虑漂移:

    Using the API is simple; there is no need for complex parsing or accounting for drift on the back-end:

    using Windows.System.Profile;
    
    IBuffer GetSystemId()
    {
      // This sample gets the publisher ID which is the same for all apps
      // by this publisher on this device.
      // Use GetSystemIdForUser if you have the userSystemId capability
      // and need the same ID across all apps for this user (not 
      // really applicable for apps in the Windows Store)
      var systemId = SystemIdentification.GetSystemIdForPublisher();
    
      // Make sure this device can generate the IDs
      if (systemId.Source != SystemIdentificationSource.None)
      {
        // The Id property has a buffer with the unique ID
        return systemId.Id;
      }
    
      // This is a very old PC without the correct hardware. Use 
      // another mechanism to generate an ID (or perhaps just give 
      // up due to the small number of people that won't have the ID; 
      // depends on your business needs).
      return GetIdFromAshwidOrSomethingElse();
    }
    

    如问题中所述,此 ID 应仅用于后端服务中的关联目的(例如,用于遥测、广告、使用指标等).它绝不应该用于创建匿名用户帐户、识别或跟踪用户、加密用户数据等.这是因为不同的用户可以共享同一设备,或者同一用户可以在不同的设备上漫游,所以ID 不会与用户或其数据进行 1:1 映射.

    As noted in the question, this ID should only be used for purposes of correlation in a back-end service (eg, for telemetry, advertising, usage metrics, etc.). It should never be used to create anonymous user accounts, to identify or track users, to encrypt user data, etc. This is because different users can share the same device, or the same user can roam across different devices, so the ID doesn't map 1:1 with a user or their data.

    此 API 在通用 API 合同 v3 中可用,并且可以在 Windows 通用 SDK 版本 10.0.14393.0 中找到(请记住,如果您正在开发多版本应用程序并希望点亮此 API 的使用,您应该进行运行时版本检查;而应该只查询操作系统以查看 API 是否可用).

    This API is available in the Universal API Contract v3, and can be found in the Windows Universal SDK version 10.0.14393.0 (remember that if you're doing multi-version apps and want to light-up usage of this API, you should not do runtime version check; instead you should just query the OS to see if the API is available).