I want to get the mac address of my software user on the network due to some security reasons. But i am able to get the mac address of the router through following method.
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;
}
I need the specific mac address of the system that access my software on the internet and the internal device of that router. is that possible through C#?
This Works for me.!
string macAddress = NetworkInterface
.GetAllNetworkInterfaces()
.Where( nic => nic.OperationalStatus == OperationalStatus.Up && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback )
.Select( nic => nic.GetPhysicalAddress().ToString() )
.FirstOrDefault();
static string GetMacAddress()
{
string macAddresses = "";
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) {
// Only consider Ethernet network interfaces, thereby ignoring any
// loopback devices etc.
if (nic.NetworkInterfaceType != NetworkInterfaceType.Ethernet) continue;
if (nic.OperationalStatus == OperationalStatus.Up) {
macAddresses += nic.GetPhysicalAddress().ToString();
break;
}
}
return macAddresses;
}
Taken from here , just one quick google away.
This method is the best way to get Mac address of your User.
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
private static string GetClientMAC(string strClientIP)
{
string mac_dest = "";
try
{
Int32 ldest = inet_addr(strClientIP);
Int32 lhost = inet_addr("");
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest, 0, ref macinfo, ref len);
string mac_src = macinfo.ToString("X");
while (mac_src.Length < 12)
{
mac_src = mac_src.Insert(0, "0");
}
for (int i = 0; i < 11; i++)
{
if (0 == (i % 2))
{
if (i == 10)
{
mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
}
else
{
mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
}
}
}
}
catch (Exception err)
{
throw new Exception("Lỗi " + err.Message);
}
return mac_dest;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.