简体   繁体   中英

How to get a mac address

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();
                break;
            }
        }

        return macadress;

now how can get mac address users???

2. how can get ip users???

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.

您无法从请求中获取 MAC 地址,但是,您可以通过Request.UserHostAddress获取 IP

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;
            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";
            }
        }

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM