如何获得系统的唯一标识符 [英] How to GET Unique identifier of system

查看:117
本文介绍了如何获得系统的唯一标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要系统的唯一标识符。我用进程ID,但它也并非是独一无二的所有PC。所以我得到CPU的序列号。

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.

我使用WMI获取序列号。
// Win32_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
}



但它返回要通过OEM

为什么它不返回精确序列号填写。

Why It does not returns exact serial number.

请让我知道,我怎么能解决这个问题。

Please Let me know How can I Fix this.

推荐答案

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

I've needed to solve the same issue before.

完成MAC地址的事情,说破可怕的是我们很快就发现只是10K用户准备的几个副本。我们有问题的那一天去世产品

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.

另外一个项目需要一个类似的解决方案,我们尝试了NTFS ID为C:\驱动器。我们抛出了这个想法,然后考虑Win32的机器ID,那当然也得到了扔出去。

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.

最后我们决定到以下几点:我们创建一个GUID,加密之后与RSA机键,并坚持它在注册表中。

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;
    }
}

这篇关于如何获得系统的唯一标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆