在客戶端認證的過程中,我們總要獲取客戶機的唯一識別資訊,曾經以為mac地址是不會變的,但是現在各種改,特別是使用無線上網絡卡,mac地址插一次變一次,所以這樣使用mac就沒有什麼意義了,怎麼辦,又開始求助google,最後找到一個折中的方案
通過獲取主機板、處理器、bios、mac、顯示卡、硬碟等的id生成唯一識別碼
1、使用那些不經常更換的模組來生成識別碼。
2、如果經常更換mac,顯示卡,硬碟,則不要使用這些id。
3、確保使用static變數在整個應用來儲存唯一識別碼。
注意引用system.management
using system;參考using system.management;
using system.security.cryptography;
using system.security;
using system.collections;
using system.text;
namespace security
return fingerprint;
}private static string gethash(string s)
private static string gethexstring(byte bt)
return s;
}#region original device id getting code
//return a hardware identifier
private static string identifier
(string wmiclass, string wmiproperty, string wmimustbetrue)
catch}}
}return result;
}//return a hardware identifier
private static string identifier(string wmiclass, string wmiproperty)
catch}}
return result;
}private static string cpuid()
//add clock speed for extra security
retval += identifier("win32_processor", "maxclockspeed");}}
return retval;
}//bios identifier
private static string biosid()
//main physical hard drive id
private static string diskid()
//motherboard id
private static string baseid()
//primary video controller id
private static string videoid()
//first enabled network card id
private static string macid()
#endregion}}
現在遇到一些平板等簡陋的機型,竟然獲取到的所有裝置標識都一樣(除了mac),最後只好在本地再生成一個軟體自身的標識,然後每次在計算標識的時候附帶上,這樣不會再重複了吧。
**如下:
private static string localkey()可以再把該檔案設為隱藏等手段,防止使用者誤操作。else
}
檔案容易被誤刪,還可以寫入登錄檔,除非系統重灌,但是需要以管理員許可權執行
1 class registryhelper**:c#獲取機器唯一識別碼 - leestar54 - (cnblogs.com)2
15 }
16 return ret;
17 }
18 19 public static void setdeviceid()
20
29 30 string id = sbuilder.tostring();
31 using (var tempk = registry.localmachine.createsubkey(_urideviecid))
32
35 }
36 }
37 }