Take the 2-minute tour ×

Can I get a MAC address that are connected to my site.

this code get mac address host and return error permission.

  String macadress = string.Empty;

        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            OperationalStatus ot = nic.OperationalStatus;
            if (nic.OperationalStatus == OperationalStatus.Up)
                macadress = nic.GetPhysicalAddress().ToString();

        return macadress;

now how can get mac address users???

2. how can get ip users???

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

Unfortunately you can't get the user's MAC address in the way that you want. It's my understanding that MAC addresses are stripped from packets when they leave your local network.

You can try to get the user's address from Request.UserHostAddress. However if you are behind a load balancer or content distribution network then you might want to try looking in Request.Headers["X-Forwarded-For"] first - this is where the users original IP address will often be written when the request is forwarded on by something.

The approach I'll usually take is to try something along the lines of:

var address = Request.Headers["X-Forwarded-For"];
if (String.IsNullOrEmpty(address))
    address = Request.UserHostAddress;

The last project I worked on, we actually logged both, in case the forwarded for header had been faked.

share|improve this answer
Unfortunately? –  SLaks Jul 29 '11 at 13:44
Unfortunately for GodIsLive ;) –  Mike Goatly Jul 29 '11 at 13:45
MAC addresses are a feature of the Local Area Network - the MAC address you would see in a packet would be from your router, because that is the thing in your LAN that forwarded that packet to you. –  Hafthor Feb 11 at 17:38
add comment

You cannot get the MAC address from the request, however, you can get the IP with Request.UserHostAddress

share|improve this answer
add comment

You cannot get the MAC address of the end-user's machine.

You can get the user's public IP address using Request.UserHostAddress.

Note the IP address this will not be unique per-user.
If multiple users are behind the same proxy or are on a corporate network, they will usually share the same address.
You can check the X-Forwarded-For header to get a little more information.
Note that this header can be chained or faked.

share|improve this answer
add comment
public string GetMacAddress(string ipAddress)
            string macAddress = string.Empty;
            System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
            pProcess.StartInfo.FileName = "arp";
            pProcess.StartInfo.Arguments = "-a " + ipAddress;
            pProcess.StartInfo.UseShellExecute = false;
            pProcess.StartInfo.RedirectStandardOutput = true;
             pProcess.StartInfo.CreateNoWindow = true;
            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;

                return "not found";
share|improve this answer
add comment

Your Answer


