最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c# 獲取機器唯一識別碼的示例

 更新時間:2021年03月11日 09:28:01   作者:leestar54  
這篇文章主要介紹了c# 獲取機器唯一識別碼的示例,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下

前言

在客戶端認證的過程中,我們總要獲取客戶機的唯一識別信息,曾經(jīng)以為MAC地址是不會變的,但是現(xiàn)在各種改,特別是使用無線上網(wǎng)卡,MAC地址插一次變一次,所以這樣使用MAC就沒有什么意義了,怎么辦,又開始求助Google,最后找到一個折中的方案

原理

通過獲取主板、處理器、BIOS、mac、顯卡、硬盤等的ID生成唯一識別碼

建議

1、使用那些不經(jīng)常更換的模塊來生成識別碼。

2、如果經(jīng)常更換MAC,顯卡,硬盤,則不要使用這些ID。

3、確保使用static變量在整個應用來保存唯一識別碼。

實現(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
 }
}

補充

現(xiàn)在遇到一些平板等簡陋的機型,竟然獲取到的所有設備標識都一樣(除了mac),最后只好在本地再生成一個軟件自身的標識,然后每次在計算標識的時候附帶上,這樣不會再重復了吧。

代碼如下:

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

可以再把該文件設為隱藏等手段,防止用戶誤操作。

補充2

文件容易被誤刪,還可以寫入注冊表,除非系統(tǒng)重裝,但是需要以管理員權(quá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# 獲取機器唯一識別碼的示例的詳細內(nèi)容,更多關(guān)于c# 獲取機器識別碼的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#實現(xiàn)文本讀取的7種方式

    C#實現(xiàn)文本讀取的7種方式

    這篇文章主要介紹了C#實現(xiàn)文本讀取的7種方式,文本讀取在上位機開發(fā)中經(jīng)常會使用到,實現(xiàn)的方式也有很多種,下面我們就來分享七種方式,需要的小伙伴可以參考一下
    2022-05-05
  • C#/.Net開發(fā)chatGPT、openAI的簡單步驟

    C#/.Net開發(fā)chatGPT、openAI的簡單步驟

    OpenAI處于科技行業(yè)下一件大事件的最前沿,具有初創(chuàng)公司史詩般的標志,下面這篇文章主要給大家介紹了關(guān)于C#/.Net開發(fā)chatGPT和openAI的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • C#編程自學之運算符和表達式

    C#編程自學之運算符和表達式

    這篇文章主要介紹了C#運算符和表達式,這是自學C#編程的第五篇,希望對大家的學習有所幫助。
    2015-10-10
  • c#中object、var和dynamic的區(qū)別小結(jié)

    c#中object、var和dynamic的區(qū)別小結(jié)

    這篇文章主要給大家介紹了關(guān)于c#中object、var和dynamic的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • C#發(fā)送內(nèi)置圖片html格式郵件的方法

    C#發(fā)送內(nèi)置圖片html格式郵件的方法

    這篇文章主要介紹了C#發(fā)送內(nèi)置圖片html格式郵件的方法,涉及C#發(fā)送郵件的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • unity實現(xiàn)鼠標經(jīng)過時ui及物體的變色操作

    unity實現(xiàn)鼠標經(jīng)過時ui及物體的變色操作

    這篇文章主要介紹了unity實現(xiàn)鼠標經(jīng)過時ui及物體的變色操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • C#?OpenCV實現(xiàn)形狀匹配的方法詳解

    C#?OpenCV實現(xiàn)形狀匹配的方法詳解

    這篇文章主要為大家詳細介紹了如何利用C#+OpenCV實現(xiàn)形狀匹配的效果,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2022-09-09
  • C#?wpf嵌入winform控件的示例詳解

    C#?wpf嵌入winform控件的示例詳解

    wpf的強大界面能力,再加上winform的性能以及靈活性,那基本上什么界面都能夠做的很好,本文我們就來看看如何在C#?wpf中嵌入winform控件吧
    2024-03-03
  • C#快速配置NLog日志的教程詳解

    C#快速配置NLog日志的教程詳解

    這篇文章主要為大家詳細介紹了C#快速配置NLog日志的教程相關(guān)知識,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以了解一下
    2024-02-02
  • C#開發(fā)Winform程序調(diào)用存儲過程

    C#開發(fā)Winform程序調(diào)用存儲過程

    這篇文章介紹了C#開發(fā)Winform程序調(diào)用存儲過程的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05

最新評論

遂川县| 石柱| 牙克石市| 屏东县| 高淳县| 阳曲县| 兴国县| 银川市| 德江县| 洪江市| 壶关县| 莫力| 喀喇沁旗| 剑川县| 白银市| 伊宁县| 龙川县| 濮阳市| 汉中市| 龙山县| 多伦县| 文山县| 周宁县| 阿合奇县| 乌海市| 筠连县| 乌拉特后旗| 濮阳市| 都江堰市| 克东县| 高唐县| 贵阳市| 台东县| 辽中县| 祁连县| 洪洞县| 海伦市| 云龙县| 昌都县| 从江县| 织金县|