October 7, 2024

MAC Address in C#?

This article describes how to get user MAC address using C#? or C# code to determine a visitor’s MAC address. Summary of the article:

  • What is MAC Address?
  • C# Code to Get Visitor/Client MAC Address

What is MAC Address?
MAC or Media Access Control address is a unique identifier assigned to network interfaces for communications on the physical network segment. It is used as a network address for most IEEE 802 network technologies. It is used by the Media Access Control sub layer of the Data-Link Layer (DLC) of communication protocols. MAC address is unique for each physical device.
The standard (IEEE 802) format of a MAC addresses is six groups of two hexadecimal digits, separated by hyphens (-) or colons (:).

MAC address is also called as networking hardware address or the burned-in address (BIA) or the physical address.An  example of a MAC address is  02-23-45-67-89-bb   or   02:23:45:67:89: bb.

C# Code to Get Visitor/Client MAC Address
Sometimes we need to get the MAC address of the client machine. We collect this MAC address for several purposes like: security purposes, to identify the visitors, during login times etc.
By using the following C# code we can get the MAC address:

NetworkInterface[] nif = NetworkInterface.GetAllNetworkInterfaces();
String MACAddress = string.Empty;
foreach (NetworkInterface adapter in nif)
{
    if (MACAddress == String.Empty)
    {
        IPInterfaceProperties ipproperties = adapter.GetIPProperties();
        MACAddress = adapter.GetPhysicalAddress().ToString();
    }
}

For this System.Net.NetworkInformation namespace may required.

In this way we can collect the Clint MAC address in C#.

Rashedul Alam

I am a software engineer/architect, technology enthusiast, technology coach, blogger, travel photographer. I like to share my knowledge and technical stuff with others.

View all posts by Rashedul Alam →

Leave a Reply

Your email address will not be published. Required fields are marked *