0

I need to read a machine's MAC id in C# which stays fixed irrespective of the connection types e.g. connected to Work through network cable, wifi, VPN through Home wifi, through a Dongle or even can be offline.

So basically it need not necessarily be the MAC id of network interface which is "Up", i just need the MAC id which is/stays constant.

With the help of answers in the below link and other suggestions,

Reliable method to get machine's MAC address in C#

I am able to read the MAC ids, but still don't understand which one to consider that will reliably give me the fixed MAC id, which i can use in my application for some sort of verification in that particular system.

Here are the details of all network interfaces of that system, when the system is connected to "Work" through VPN using Home Wifi and connected to "Work" network directly:

Details of all network interfaces of that system

Please suggest which one i should consider and what should be the right condition to filter out the interface with fixed MAC id in C#.

Pankaj_Sen
  • 61
  • 9
  • 4
    Define what you consider to be a machine's fixed MAC address. As noted, a MAC address is assigned to a network interface, not to a "machine". So in case I have a laptop with a WiFi adapter and a wired connection, which of these two do you consider to be fixed? – Robby Cornelissen Jan 18 '21 at 07:45
  • Thanks for you answer, yes i understood the point that the MAC id is for NIC, and not for the machine. But the laptop/machine is managed by the organization, and not by user in this case. So, user doesn't bother what is kept fixed in this system by the IT team in the organization. – Pankaj_Sen Jan 18 '21 at 07:54
  • Not sure that you're trying to do. What about generating a UUID and store it in a file in the hard drive? – user202729 Jan 18 '21 at 07:58
  • How about something else - https://stackoverflow.com/questions/15723793/i-am-trying-to-get-an-unique-cpu-id – Caius Jard Jan 18 '21 at 08:22
  • @CaiusJard, thanks for suggesting another way of getting a unique id, but at the moment I am not in a position to switch to new way of getting a unique id as there are other agreements/process involved mentioning the MAC id.... – Pankaj_Sen Jan 18 '21 at 10:08
  • I asked this question as i was unable to figure out the physical MAC id of the NIC (or more precisely was not sure on how to filter out that in C#), and didn't get any hint from the existing similar questions.....but don't know why this negative vote for my question.. – Pankaj_Sen Jan 18 '21 at 10:11
  • Some parameter has to be applied - like if your company always buys laptops with intel wireless chipsets then search for "intel" and "wireless" in the data.. – Caius Jard Jan 18 '21 at 14:23
  • 1
    Tempted to say this Q is a duplicate of https://stackoverflow.com/questions/1567377/how-to-determine-mac-address-of-the-actual-physical-network-card-not-virtual?rq=1 – Caius Jard Jan 18 '21 at 14:25
  • @CaiusJard thanks for hint for checking chipsets, but this would not work for me as the application will be used in other companies too. – Pankaj_Sen Jan 19 '21 at 06:50
  • @CaiusJard, yes i saw the link you gave in "Related" section after i posted my query, sorry about that...but it helped me to find my solution. Thanks again. I am now posting my solution here, please comment if you find any further drawback. – Pankaj_Sen Jan 19 '21 at 06:53

2 Answers2

1

As indicated as "Related" to my question and also as pointed by @Caius Jard, i got the solution from the below link which will suit for my need:

How to determine MAC Address of the actual physical network card -- not virtual network interfaces created by VPN's (.NET C#)

because i want to: -> take the MAC id of the physical card (i.e. excluding all virtual, pseudo, logical, usb) and don't allow my application without any physical card -> consider the 1st card, in case more than 1 cards are installed

I have modified the query as per my need with the help of details given in below link:

https://weblogs.sqlteam.com/mladenp/2010/11/04/find-only-physical-network-adapters-with-wmi-win32_networkadapter-class/

So, my final solution looks like below:

ManagementObjectSearcher searcher = new ManagementObjectSearcher
            (@"SELECT Index,MACAddress,PNPDeviceID 
            FROM Win32_NetworkAdapter 
            WHERE MACAddress IS NOT NULL AND PNPDeviceID IS NOT NULL AND Manufacturer != 'Microsoft' AND PNPDeviceID LIKE '%PCI\\%'");

            IList<ManagementObject> mObject = searcher.Get()
                .Cast<ManagementObject>()
                .OrderBy(p => Convert.ToUInt32(p.Properties["Index"].Value))
                .ToList();

            foreach (var obj in mObject)
            {
                var mac = obj["MACAddress"].ToString();
                mac = mac.Replace(":", string.Empty);
                return mac;
            }
Pankaj_Sen
  • 61
  • 9
0

Your physical (vs. virtual, logical) interface MAC addresses should stay same (at least until the physical device is replaced). Your "Ethernet 3", for example.

Qareke
  • 30
  • 9
  • Yes, it should be, but i can't see it ("Ethernet 3") listed on the left side of the attached image. Am i missing your point? – Pankaj_Sen Jan 18 '21 at 08:16
  • On the right side, as I see it. Physical interface usually doesn't have "virtual", "pseudo", "logical" in their description. They have description of the device. For example, you have "Wi-Fi, Intel(R) 6 AX201 160MHz" on the left side – Qareke Jan 18 '21 at 08:25
  • ok, this is the physical interface for this particular system, but how to find out the physical interface in a more general way? I mean in the image if you see, there is an interface e.g. "Bluetooth Network Connection" for which also the description doesn't have "virtual", "pseudo", "logical". – Pankaj_Sen Jan 18 '21 at 10:26
  • So, Bluetooth should be a physical device. It should have same MAC on both sides of the image (assuming it is the same computer). You can use it, if you just want uniquely ID the computer. Is that what you want? – Qareke Jan 18 '21 at 10:55
  • 1
    By the way, "Ethernet 3" in my answer is not a very good example, as the device looks like a USB device. – Qareke Jan 18 '21 at 10:57
  • Yes "Ethernet 3" should not be, even if i exclude ("virtual", "pseudo", "logical" and "usb") but there are still 2 options ("Intel(R)...." and "Bluetooth..."), both are physical interface. Is it good idea to take the 1st one and exit....i am concerned about the order of these. To give little more hint of my need, i will read that MAC id, generate some code using this and give to the user to use it to enable something in the application...for that reason, my logic should always return the same MAC id for that particular computer (without any change of internal hardware, e.g. NIC) – Pankaj_Sen Jan 18 '21 at 11:17
  • Again, if you just want to identify computers, it should work. MAC address of a physical device is "etched" in. It should stay same unless it is replaced or been spoofed. Other possible way to make sure, if you have same brand of laptops/NICs across your company, you can read the first 6 hex-digits of the MAC address. They are assigned to specific manufacturer and should be same for the same brand – Qareke Jan 18 '21 at 11:28
  • I have added an answer, please check if you find any further drawback. Thanks again for all your hints. – Pankaj_Sen Jan 19 '21 at 07:02
  • your solution will work only on PCI interface. I am not sure if, let say, a WiFi laptop adapter connected on m.2 interface – Qareke Jan 19 '21 at 07:34
  • Okay, that's new for me. Do you mean passive PCI-E M.2 card adapter (i mean if M.2 slot is PCIe based)? I think then it should not be a problem. What do you say? – Pankaj_Sen Jan 19 '21 at 11:34
  • It is PCIe. You are right. It could be NVMe, but that is PCIe bus as well, so it might work. – Qareke Jan 19 '21 at 13:40