c# 獲取機(jī)器唯一識(shí)別碼的示例
前言
在客戶端認(rèn)證的過(guò)程中,我們總要獲取客戶機(jī)的唯一識(shí)別信息,曾經(jīng)以為MAC地址是不會(huì)變的,但是現(xiàn)在各種改,特別是使用無(wú)線上網(wǎng)卡,MAC地址插一次變一次,所以這樣使用MAC就沒(méi)有什么意義了,怎么辦,又開(kāi)始求助Google,最后找到一個(gè)折中的方案
原理
通過(guò)獲取主板、處理器、BIOS、mac、顯卡、硬盤(pán)等的ID生成唯一識(shí)別碼
建議
1、使用那些不經(jīng)常更換的模塊來(lái)生成識(shí)別碼。
2、如果經(jīng)常更換MAC,顯卡,硬盤(pán),則不要使用這些ID。
3、確保使用static變量在整個(gè)應(yīng)用來(lái)保存唯一識(shí)別碼。
實(shí)現(xiàn)
注意引用System.Management
using System; using System.Management; using System.Security.Cryptography; using System.Security; using System.Collections; using System.Text; namespace Security { /// <summary> /// Generates a 16 byte Unique Identification code of a computer /// Example: 4876-8DB5-EE85-69D3-FE52-8CF7-395D-2EA9 /// </summary> public class FingerPrint { private static string fingerPrint = string.Empty; public static string Value() { if (string.IsNullOrEmpty(fingerPrint)) { fingerPrint = GetHash("CPU >> " + cpuId() + "\nBIOS >> " + biosId() + "\nBASE >> " + baseId() //+"\nDISK >> "+ diskId() + "\nVIDEO >> " + videoId() +"\nMAC >> "+ macId() ); } return fingerPrint; } private static string GetHash(string s) { MD5 sec = new MD5CryptoServiceProvider(); ASCIIEncoding enc = new ASCIIEncoding(); byte[] bt = enc.GetBytes(s); return GetHexString(sec.ComputeHash(bt)); } private static string GetHexString(byte[] bt) { string s = string.Empty; for (int i = 0; i < bt.Length; i++) { byte b = bt[i]; int n, n1, n2; n = (int)b; n1 = n & 15; n2 = (n >> 4) & 15; if (n2 > 9) s += ((char)(n2 - 10 + (int)'A')).ToString(); else s += n2.ToString(); if (n1 > 9) s += ((char)(n1 - 10 + (int)'A')).ToString(); else s += n1.ToString(); if ((i + 1) != bt.Length && (i + 1) % 2 == 0) s += "-"; } return s; } #region Original Device ID Getting Code //Return a hardware identifier private static string identifier (string wmiClass, string wmiProperty, string wmiMustBeTrue) { string result = ""; System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass); System.Management.ManagementObjectCollection moc = mc.GetInstances(); foreach (System.Management.ManagementObject mo in moc) { if (mo[wmiMustBeTrue].ToString() == "True") { //Only get the first one if (result == "") { try { result = mo[wmiProperty].ToString(); break; } catch { } } } } return result; } //Return a hardware identifier private static string identifier(string wmiClass, string wmiProperty) { string result = ""; System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass); System.Management.ManagementObjectCollection moc = mc.GetInstances(); foreach (System.Management.ManagementObject mo in moc) { //Only get the first one if (result == "") { try { result = mo[wmiProperty].ToString(); break; } catch { } } } return result; } private static string cpuId() { //Uses first CPU identifier available in order of preference //Don't get all identifiers, as it is very time consuming string retVal = identifier("Win32_Processor", "UniqueId"); if (retVal == "") //If no UniqueID, use ProcessorID { retVal = identifier("Win32_Processor", "ProcessorId"); if (retVal == "") //If no ProcessorId, use Name { retVal = identifier("Win32_Processor", "Name"); if (retVal == "") //If no Name, use Manufacturer { retVal = identifier("Win32_Processor", "Manufacturer"); } //Add clock speed for extra security retVal += identifier("Win32_Processor", "MaxClockSpeed"); } } return retVal; } //BIOS Identifier private static string biosId() { return identifier("Win32_BIOS", "Manufacturer") + identifier("Win32_BIOS", "SMBIOSBIOSVersion") + identifier("Win32_BIOS", "IdentificationCode") + identifier("Win32_BIOS", "SerialNumber") + identifier("Win32_BIOS", "ReleaseDate") + identifier("Win32_BIOS", "Version"); } //Main physical hard drive ID private static string diskId() { return identifier("Win32_DiskDrive", "Model") + identifier("Win32_DiskDrive", "Manufacturer") + identifier("Win32_DiskDrive", "Signature") + identifier("Win32_DiskDrive", "TotalHeads"); } //Motherboard ID private static string baseId() { return identifier("Win32_BaseBoard", "Model") + identifier("Win32_BaseBoard", "Manufacturer") + identifier("Win32_BaseBoard", "Name") + identifier("Win32_BaseBoard", "SerialNumber"); } //Primary video controller ID private static string videoId() { return identifier("Win32_VideoController", "DriverVersion") + identifier("Win32_VideoController", "Name"); } //First enabled network card ID private static string macId() { return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled"); } #endregion } }
補(bǔ)充
現(xiàn)在遇到一些平板等簡(jiǎn)陋的機(jī)型,竟然獲取到的所有設(shè)備標(biāo)識(shí)都一樣(除了mac),最后只好在本地再生成一個(gè)軟件自身的標(biāo)識(shí),然后每次在計(jì)算標(biāo)識(shí)的時(shí)候附帶上,這樣不會(huì)再重復(fù)了吧。
代碼如下:
private static string localkey() { string path=Environment.CurrentDirectory + "client.key"; if (File.Exists(path)) { StreamReader sr = new StreamReader(path); string key= sr.ReadLine(); sr.Close(); return key; } else { StreamWriter sw = File.CreateText(path); string key = Guid.NewGuid().ToString(); sw.WriteLine(key); sw.Close(); return key; } }
可以再把該文件設(shè)為隱藏等手段,防止用戶誤操作。
補(bǔ)充2
文件容易被誤刪,還可以寫(xiě)入注冊(cè)表,除非系統(tǒng)重裝,但是需要以管理員權(quán)限運(yùn)行
class RegistryHelper { const string _uriDeviecId = "SOFTWARE\\YourCompany\\YouApp"; public static string GetDeviceId() { string ret = string.Empty; using (var obj = Registry.LocalMachine.OpenSubKey(_uriDeviecId, false)) { if (obj != null) { var value = obj.GetValue("DeviceId"); if (value != null) ret = Convert.ToString(value); } } return ret; } public static void SetDeviceId() { using (MD5 md5Hash = MD5.Create()) { byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(DateTime.Now.ToString())); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } string id = sBuilder.ToString(); using (var tempk = Registry.LocalMachine.CreateSubKey(_uriDeviecId)) { tempk.SetValue("DeviceId", id); } } } }
以上就是c# 獲取機(jī)器唯一識(shí)別碼的示例的詳細(xì)內(nèi)容,更多關(guān)于c# 獲取機(jī)器識(shí)別碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#如何提取經(jīng)緯度文件中的經(jīng)緯度數(shù)據(jù)
近期開(kāi)發(fā)時(shí)需要獲取當(dāng)前的經(jīng)緯度坐標(biāo),下面這篇文章主要給大家介紹了關(guān)于C#如何提取經(jīng)緯度文件中經(jīng)緯度數(shù)據(jù)的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08C#區(qū)分中英文按照指定長(zhǎng)度截取字符串的方法
這篇文章主要介紹了C#區(qū)分中英文按照指定長(zhǎng)度截取字符串的方法,涉及C#操作字符串的正則匹配與截取等常用操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03Unity?UGUI的PointerEventData的介紹及使用
這篇文章主要為大家介紹了Unity?UGUI的PointerEventData的介紹及使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07C#簡(jiǎn)單獲取全屏中鼠標(biāo)焦點(diǎn)位置坐標(biāo)的方法示例
這篇文章主要介紹了C#簡(jiǎn)單獲取全屏中鼠標(biāo)焦點(diǎn)位置坐標(biāo)的方法,涉及C#針對(duì)鼠標(biāo)位置Position屬性的簡(jiǎn)單操作技巧,需要的朋友可以參考下2017-07-07C#如何自動(dòng)選擇出系統(tǒng)中最合適的IP地址
這篇文章介紹了C#如何自動(dòng)選擇出系統(tǒng)中最合適的IP地址,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-08-08C#實(shí)現(xiàn)多種圖片格式轉(zhuǎn)換的示例詳解
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)多種圖片格式轉(zhuǎn)換,例如轉(zhuǎn)換成圖標(biāo)圖像ICO,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01利用Aspose.Word控件實(shí)現(xiàn)Word文檔的操作
偶然一次機(jī)會(huì),一個(gè)項(xiàng)目的報(bào)表功能指定需要導(dǎo)出為Word文檔,因此尋找了很多篇文章,不過(guò)多數(shù)介紹的比較簡(jiǎn)單一點(diǎn),于是也參考了官方的幫助介紹,終于滿足了客戶的需求。下面我由淺入深來(lái)介紹這個(gè)控件在實(shí)際業(yè)務(wù)中的使用過(guò)程吧2013-05-05