如何获取系统的唯一标识符

发布于 2024-09-25 02:49:52 字数 422 浏览 1 评论 0原文

我需要系统的唯一标识符。我使用了 processid,但它也不是对所有 PC 都是唯一的。这样我就得到了CPU的序列号。

我正在使用 WMI 来获取序列号。 // Win32_CPU

var search = new ManagementObjectSearcher( "SELECT * FROM Win32_baseboard" );
var mobos = search.Get();

foreach (var m in mobos)
{
  var serial = m["SerialNumber"]; // ProcessorID if you use Win32_CPU
}

但它返回“由 OEM 填写

为什么它不返回准确的序列号。

请让我知道如何解决这个问题。

I need Unique Identifier of the system. I used processid but it also not unique for all PCs. So I get serial number of CPU.

I am using WMI to get serial number.
// Win32_CPU

var search = new ManagementObjectSearcher( "SELECT * FROM Win32_baseboard" );
var mobos = search.Get();

foreach (var m in mobos)
{
  var serial = m["SerialNumber"]; // ProcessorID if you use Win32_CPU
}

But It returns "To be filled by O.E.M."

Why It does not returns exact serial number.

Please Let me know How can I Fix this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

余生再见 2024-10-02 02:49:52

我之前需要解决同样的问题。

完成 MAC 地址的事情后,结果非常糟糕,因为我们很快就在 10k 用户身上发现了多个重复项。直到产品失效的那一天我们都遇到了问题。

另一个项目需要类似的解决方案,我们尝试了 C:\ 驱动器的 NTFS Id。我们抛弃了这个想法,然后考虑了 Win32 机器 ID,当然也被抛弃了。

最后我们决定如下:我们创建一个 Guid,使用 RSA 机器密钥对其进行加密,并将其粘贴到注册表中。

我真的相信这是实现独特性的最佳选择,即使您还需要合并一些硬件信息。所以这就像我们用来存储 & 的代码一样。拿来:

static class MachineKey
{
    const byte[] ENTROPY = null;
    const string KEY_PATH = @"HKEY_LOCAL_MACHINE\SOFTWARE\company-name-goes-here";

    public static Guid Get()
    {
        try
        {
            string base64 = Microsoft.Win32.Registry.GetValue(KEY_PATH, "HostId", null) as string;
            if (base64 == null) 
                return Create();

            byte[] cypher = System.Convert.FromBase64String(base64);
            byte[] bytes = System.Security.Cryptography.ProtectedData.Unprotect(cypher, ENTROPY, 
                System.Security.Cryptography.DataProtectionScope.LocalMachine);

            if (bytes.Length != 20 || bytes[0] != 'H' || bytes[1] != 'o' || bytes[2] != 's' || bytes[3] != 't')
                return Create();

            byte[] guid = new byte[16];
            Array.Copy(bytes, 4, guid, 0, 16);
            return new Guid(guid);
        }
        catch
        {
            return Create();
        }
    }

    static Guid Create()
    {
        byte[] prefix = new byte[] { (byte)'H', (byte)'o', (byte)'s', (byte)'t' };
        Guid id = Guid.NewGuid();
        byte[] store = new byte[20];
        Array.Copy(prefix, store, 4);
        Array.Copy(id.ToByteArray(), 0, store, 4, 16);

        store = System.Security.Cryptography.ProtectedData.Protect(store, ENTROPY,
            System.Security.Cryptography.DataProtectionScope.LocalMachine);

        Microsoft.Win32.Registry.SetValue(KEY_PATH, "HostId", System.Convert.ToBase64String(store));
        return id;
    }
}

I've needed to solve the same issue before.

Done the MAC address thing, that broke horribly as we quickly found several duplicates on just 10k users. We had problems to the day that product died.

Another project needed a similar solution and we tried the NTFS Id for C:\ drive. We threw out that idea and then considered the Win32 machine id, that of course also got thrown out.

Finally we settled onto the following: We created a Guid, encrypted it with the RSA machine key, and stuck it in the Registry.

I really believe that is your best bet for uniqueness even if you then need to incorporate some hardware information as well. So here is something like the code we used to store & fetch:

static class MachineKey
{
    const byte[] ENTROPY = null;
    const string KEY_PATH = @"HKEY_LOCAL_MACHINE\SOFTWARE\company-name-goes-here";

    public static Guid Get()
    {
        try
        {
            string base64 = Microsoft.Win32.Registry.GetValue(KEY_PATH, "HostId", null) as string;
            if (base64 == null) 
                return Create();

            byte[] cypher = System.Convert.FromBase64String(base64);
            byte[] bytes = System.Security.Cryptography.ProtectedData.Unprotect(cypher, ENTROPY, 
                System.Security.Cryptography.DataProtectionScope.LocalMachine);

            if (bytes.Length != 20 || bytes[0] != 'H' || bytes[1] != 'o' || bytes[2] != 's' || bytes[3] != 't')
                return Create();

            byte[] guid = new byte[16];
            Array.Copy(bytes, 4, guid, 0, 16);
            return new Guid(guid);
        }
        catch
        {
            return Create();
        }
    }

    static Guid Create()
    {
        byte[] prefix = new byte[] { (byte)'H', (byte)'o', (byte)'s', (byte)'t' };
        Guid id = Guid.NewGuid();
        byte[] store = new byte[20];
        Array.Copy(prefix, store, 4);
        Array.Copy(id.ToByteArray(), 0, store, 4, 16);

        store = System.Security.Cryptography.ProtectedData.Protect(store, ENTROPY,
            System.Security.Cryptography.DataProtectionScope.LocalMachine);

        Microsoft.Win32.Registry.SetValue(KEY_PATH, "HostId", System.Convert.ToBase64String(store));
        return id;
    }
}
﹉夏雨初晴づ 2024-10-02 02:49:52

组装计算机的 OEM 从未输入序列号。

要修复此问题,您可以构建自己的计算机并正确输入序列号。

The OEM that assembled the computer never put in a serial number.

To fix it, you can build your own computer and put in the serial number correctly.

烟沫凡尘 2024-10-02 02:49:52

您尝试过 Win32_BIOS 吗?

Did you try at Win32_BIOS ?

烟花易冷人易散 2024-10-02 02:49:52

“我需要系统的唯一标识符。”

常见的方法是使用网卡的 MAC。它可以改变,但对于大多数用途来说已经足够了。

public PhysicalAddress GetMacAddress()
{
    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        if (nic.OperationalStatus == OperationalStatus.Up)
        {
            return nic.GetPhysicalAddress();
        }
    }

    return null;
}

您还可以尝试 Win32_Processor、Win32_Bios 或组合。我的意思是连接来自系统的多个组件的标识符,为系统生成唯一的标识符,因此,如果组件标识符之一不唯一,那并不重要。这当然有一个问题,如果组件更改,唯一标识符也会更改。

"I need Unique Identifier of the system."

A common approach is to use the MAC of a network card. It can be altered but for most purposes its good enough.

public PhysicalAddress GetMacAddress()
{
    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        if (nic.OperationalStatus == OperationalStatus.Up)
        {
            return nic.GetPhysicalAddress();
        }
    }

    return null;
}

You could also try Win32_Processor, Win32_Bios or combination. By which I mean concat identifiers from several components of the system to generate a unique identifier for the system so it doesnt matter if for instance one of the component identifier isn't unique. This of course has the problem that if a component changes the unique identifier changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文