I would like to know the best way to get the client MAC Address from my Asp.Net Web Forms application. Since it's low-level information, I do not think it's as simple as it sounds.
I would like to know the best way to get the client MAC Address from my Asp.Net Web Forms application. Since it's low-level information, I do not think it's as simple as it sounds.
It is not possible to get this information, since neither asp.net has access to this client machine information nor Javascript, as it is not trafficked as part of the Http protocol.
The only way to get this information would be through an ActiveX component or Java applet that would need to be installed on the client machine and the appropriate security settings applied.
If your clients are on the same LAN as you (for example, on an intranet), your MAC addresses may be present in the router's ARP table.
In a Windows environment, you can check the ARP table via ARP -a
command:
TheSystem.Diagnostics.Process
classallowsyoutorunaprocessonyourserver;optionally,youcanredirectthegeneratedcontenttoatextstream(viaStartInfo.RedirectStandardOutput=true
).
ThefunctionbelowperformsthedescribedstepstoobtainMACAddressfromanIPaddress:
publicstringGetMacAddress(stringipAddress){stringmacAddress=string.Empty;System.Diagnostics.ProcesspProcess=newSystem.Diagnostics.Process();pProcess.StartInfo.FileName="arp";
pProcess.StartInfo.Arguments = "-a " + ipAddress;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.Start();
string strOutput = pProcess.StandardOutput.ReadToEnd();
string[] substrings = strOutput.Split('-');
if (substrings.Length >= 8)
{
macAddress = substrings[3].Substring(Math.Max(0, substrings[3].Length - 2)) + "-" + substrings[4] + "-" + substrings[5] + "-" + substrings[6] + "-" + substrings[7] + "-" +
substrings[8].Substring(0, 2);
return macAddress;
}
else
{
return "not found";
}
}
Friends, by mistake I did not realize that it was a WEB application.
Follow the javascript solution to list the MAC addresses, however it uses an ActiveX and will only work in IE. I'll look for you if there are any multi-browser solutions.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Getting MAC Address From Javascript(IE Only)</title>
<script language="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>
</head>
<body>
<input type="button" value="Show MAC Address" onclick="showMacAddress()" />
<div id="box">
</div>
</body>
</html>