What is the MAC (Media Access Control) address?
The Media Access Control address (MAC address) is a unique identifier assigned to most network adapters or network interface cards (NICs) by the manufacturer for identification and use in the Media Access Control protocol sub-layer. If assigned by the manufacturer, a MAC address usually encodes the manufacturer's registered identification number. It may also be known as an Ethernet Hardware Address (EHA), hardware address, adapter address, or physical address.
Why do we use MAC?
MAC address is very commonly used in various applications. It is generally used in voting applications, and poll applications like this where identifying a unique user is very important. There are many ways you can identify a unique user such as using the hard disk ID, processor ID, etc for desktop applications, and for web applications, you can use an IP address, cookies, or a session state server, etc.
How to get MAC address Using C#
Now I want to share with how to get the system MAC address using C#. In C# you can get it in various ways here I have discussed two different ways.
First way
You need to import the System.Net namespace for this to work. This will support IPv4 and IPv6.
public string GetMACAddress()
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
String sMacAddress = string.Empty;
foreach (NetworkInterface adapter in nics)
{
if (sMacAddress == String.Empty)// only return MAC Address from first card
{
IPInterfaceProperties properties = adapter.GetIPProperties();
sMacAddress = adapter.GetPhysicalAddress().ToString();
}
}
return sMacAddress;
}
Second way
To implement this function you need to add System Management namespace in your application. If it is not available you need to add its reference from "Add references" from your project.
public string GetMACAddress()
{
ManagementObjectSearcher objMOS = new ManagementObjectSearcher("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMOS.Get();
string MACAddress = String.Empty;
foreach (ManagementObject objMO in objMOC)
{
if (MACAddress == String.Empty) // only return MAC Address from first card
{
MACAddress = objMO["MacAddress"].ToString();
}
objMO.Dispose();
}
MACAddress = MACAddress.Replace(":", "");
return MACAddress;
}
You might want to also add "objMO ["Caption"].ToString()" to test before the Mac address so that you know which devices you are looking at.
Also, don't forget to use try/catch blocks on these as a NullPointerReference will kill the app.
How to get the Client's MAC address(Web)
To get the client's MAC address only way we can rely on JavaScript and Active X control of Microsoft. It only works in IE if Active X is enabled for IE. As the ActiveXObject is not available with the Firefox, it's not working with the Firefox and is working fine in IE.
This script is for IE only
<script language="javascript" type="text/javascript">
function showMacAddress() {
var obj = new ActiveXObject("WbemScripting.SWbemLocator");
var s = obj.ConnectServer(".");
var properties = s.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
var e = new Enumerator(properties);
var output;
output = '<table border="0" cellpadding="5px" cellspacing="1px" bgcolor="#CCCCCC">';
output = output + '<tr bgcolor="#EAEAEA"><td>Caption</td><td>MACAddress</td></tr>';
while (!e.atEnd()) {
e.moveNext();
var p = e.item();
if (!p) continue;
output = output + '<tr bgcolor="#FFFFFF">';
output = output + '<td>' + p.Caption + '</td>';
output = output + '<td>' + p.MACAddress + '</td>';
output = output + '</tr>';
}
output = output + '</table>';
document.getElementById("box").innerHTML = output;
}
</script>
Hope that it may help other developers for rapid development. Please inform me if anyone gets anything wrong in this article.
Former memberPosted Jan 29, 2020, 10:24 AM
Not working
Jose SaizPosted Nov 26, 2018, 10:30 AM
I am coding a web app that allow mobile devices to browse the application via the internet, not as mobile app, but as regular browser navigation and I wonder if the above mentioned reading the MAC-Address would work if not, what would be your approach. Thanks.
Jose SaizPosted Nov 26, 2018, 10:28 AM
Would the above {First way:} works for all browser including the mobile devices such as iPad, iPhone, Android, Windows Surface, etc.?
Ramesh RathodPosted Sep 25, 2018, 12:57 PM
Really Good Article i liked
Jayanta SahuPosted Sep 14, 2017, 2:23 AM
Not working please help
csharpcorner MPosted Aug 31, 2017, 4:03 AM
Hi I want to get list of available wifi connections from the client side, any idea on this?
monal pastagiyaPosted Jul 27, 2017, 2:43 AM
Hii,kindly help i want the MAC Address of all the users whom using my website. i tried your code but it shows the MAC Address of where it have been deployed. i want the MAC Addresses of the user whom currently using on(mobile,tab,computer,laptop,etc any machine). please help...
Jenkins JPosted Apr 25, 2017, 10:00 AM
How to get the client mac adress.I tried with javascript but no results..
Henck De BeredoderPosted Sep 8, 2016, 3:30 AM
Here's an article on getting the local machine's MAC address in C#: http://www.independent-software.com/get-local-machines-mac-address-in-c/
Enrique AguilarOPosted Oct 6, 2015, 11:43 AM
I use the first option and not working at the time of publishing. Do you have any suggestions?
Anil KumarPosted Sep 16, 2015, 7:23 AM
nyc
Hitesh KumarPosted Jan 31, 2015, 3:45 AM
http://www.codesolution.org/?p=78
MarcoPosted Dec 20, 2012, 1:20 AM
Great article Ahsan. How can we get the client processor info. This information would do to ban some computers I do not want run an application from... Thanks
asvini sharmaPosted Jun 19, 2012, 8:24 AM
hey I am asvini
asvini sharmaPosted Jun 19, 2012, 8:20 AM
dbvcx