2017-08-16 1 views
0

J'essaie de récupérer l'adresse MAC de l'adaptateur Ethernet embarqué à partir d'un ordinateur, afin de générer un identifiant unique pour le périphérique. Voici l'approche que j'utilise.Comment récupérer l'adresse MAC Ethernet intégrée d'un ordinateur?

NetworkInterface[] ifConfig = NetworkInterface.GetAllNetworkInterfaces(); 
int maxHash = int.MinValue; 
Guid D = Guid.Empty; 

foreach (NetworkInterface net in ifConfig) 
{ 
    if (net.NetworkInterfaceType == NetworkInterfaceType.Ethernet) 
    { 
       if (maxHash < net.GetPhysicalAddress().ToString().GetHashCode()) 
       { 
        maxHash = net.GetPhysicalAddress().ToString().GetHashCode(); 

        ID = new Guid(String.Concat("00000000-0000-0000-0000-", net.GetPhysicalAddress().ToString())); 

       } 
    } 
} 

Cependant, l'adaptateur Bluetooth, adaptateur VM et plusieurs autres cartes réseau sont également du type NetworkInterfaceType.Ethernet. Comment puis-je obtenir spécifiquement l'adresse MAC de la connexion Ethernet intégrée?

Faire un contenant pour omettre cela ne serait pas possible. Toute aide serait très appréciée.

+2

Ceci est une mauvaise idée. Principalement parce que les adresses MAC ne sont PAS uniques. Cela devient encore pire si vous envisagez (cloné) des machines virtuelles. Voir https://superuser.com/a/968346 –

+0

@SteffenWinkler, il s'agit néanmoins de l'approche standard de facto utilisée par, par exemple, FlexLM. Quelle est l'alternative? –

+0

@HarryJohnston comme je l'ai dit, vous pouvez simplement générer un Guid et l'utiliser. Vous pouvez faire cela côté client puisque la chance de collision dans la réalité est inexistante. Vous ne pouvez pas utiliser le côté matériel pour identifier un ordinateur. L'une des raisons est les machines virtuelles, une autre est la modification de l'ordinateur, comme l'échange d'une carte réseau ou l'ajout d'un GPU. Changer le matériel (défectueux) changerait l'ID de cet ordinateur. –

Répondre

0

En option (pas le meilleur, mais quand même =)) - Vous pouvez essayer d'utiliser métrique. Dans la plupart des cas, la métrique du réseau à un réseau physique priorité de la carte

  using System; 
      using System.Collections.Generic; 
      using System.ComponentModel; 
      using System.Data; 
      using System.Drawing; 
      using System.Linq; 
      using System.Management; 
      using System.Net.NetworkInformation; 
      using System.Text; 
      using System.Threading.Tasks; 
      using System.Windows.Forms; 

private void button1_Click(object sender, EventArgs e) 
     { 
      listBox1.Items.Clear(); 
       ManagementObjectSearcher query = new 
       ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'"); 
       ManagementObjectCollection queryCollection = query.Get(); 
       foreach (ManagementObject mo in queryCollection) 
       { 
        if (!(mo["Description"].ToString().Contains("VM"))) 
        { 
         if (!(mo["Description"].ToString().Contains("Virtual"))) 
         { 
          if (!(mo["Description"].ToString().Contains("Hyper"))) 
          { 
           string[] addresses = (string[])mo["IPAddress"]; 

           string IPConnectionMetric = Convert.ToString(mo["IPConnectionMetric"]).Trim(); 

           foreach (string ipaddress in addresses) 
           { 

            listBox1.Items.Add(ipaddress + ";" + IPConnectionMetric); 
           } 

          } 

private void button2_Click(object sender, EventArgs e) 

{ 

    if (listBox1.Items.Count > 1) 
       {  
        int maximum = int.MinValue; 
        int minimum = int.MaxValue; 
        for (int i = 0; i < listBox1.Items.Count; i++) 

        { 
         int output = Convert.ToInt32(listBox1.Items[i].ToString().Split(';')[1]); 
         if ((int)output > maximum) 
          maximum = (int)output; 

        } 

        for (int i = 0; i < listBox1.Items.Count; i++) 
        { 
         int output = Convert.ToInt32(listBox1.Items[i].ToString().Split(';')[1]); 
         if ((int)output < maximum) 
          minimum = (int)output; 

         if (listBox1.Items[i].ToString().Contains(minimum.ToString())) 
         {  
          var minmetric = listBox1.Items[i].ToString(); 

          NetworkInterface[] ifConfig = NetworkInterface.GetAllNetworkInterfaces(); 
          int maxHash = int.MinValue; 
          Guid D = Guid.Empty; 
          foreach (NetworkInterface net in ifConfig) 
          { 
           if (net.NetworkInterfaceType == NetworkInterfaceType.Ethernet) 
           { 
            if (maxHash < net.GetPhysicalAddress().ToString().GetHashCode()) 
            { 
             maxHash = net.GetPhysicalAddress().ToString().GetHashCode(); 

             foreach (UnicastIPAddressInformation ip in net.GetIPProperties().UnicastAddresses) 
             { 

              if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) 
              { 

               if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) 
               { 
                if (ip.Address.ToString().Contains(minmetric.ToString().Split(';')[0])) 
                { 
                 var ID = new Guid(String.Concat("00000000-0000-0000-0000-", net.GetPhysicalAddress().ToString())); 
                } 
               } 
      else 
       { 

        NetworkInterface[] ifConfig = NetworkInterface.GetAllNetworkInterfaces(); 
        int maxHash = int.MinValue; 
        Guid D = Guid.Empty; 

        foreach (NetworkInterface net in ifConfig) 
        { 
         if (net.NetworkInterfaceType == NetworkInterfaceType.Ethernet) 
         { 
          if (maxHash < net.GetPhysicalAddress().ToString().GetHashCode()) 
          { 
           maxHash = net.GetPhysicalAddress().ToString().GetHashCode(); 

           var ID = new Guid(String.Concat("00000000-0000-0000-0000-", net.GetPhysicalAddress().ToString())); 

          } 
         } 
        }