I want to get the IP Address and the MAC Address of the user who login to the system (web application). I am using these to line of codes
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
I am using this method to get the MAC address, but the problem is I am getting the MAC Address of all the Adapters of the computer.
HOW do I know which one is the right one?, how to get the right MAC Address of the device that is connected to my website??
and also how to get the IP of the adapter too.
when i try it on my pc it give 8 adapters with 8 MAC Addresses
I am trying on my pc, I have a wired connection connected to IntraNet, and a wireless connected to internet.
You can never get the MAC address of a user connected to your website, when a socket connection occurs between the destination (your website's server) and a source (client's computer) you can only get the source IP address, for the MAC address it's never sent over the socket connection (Client <--> Server), in order to get the IP address of a user:
using System.Net;
Private string GetIP()
{
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
return addr[addr.Length-1].ToString();
}
source: Here
using System.Runtime.InteropServices;
[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;
}
This code will help you :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With