如何获取mac地址

发布于 2024-11-27 03:13:26 字数 541 浏览 0 评论 0原文

我可以获得连接到我的站点的 MAC 地址吗?

此代码获取 mac 地址主机并返回错误权限。

  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;

现在如何获取用户的mac地址???

2. 如何获取ip用户???

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???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冬天旳寂寞 2024-12-04 03:13:26

不幸的是,您无法按照您想要的方式获取用户的 MAC 地址。据我了解,当数据包离开本地网络时,MAC 地址会被从数据包中剥离。

您可以尝试Request.UserHostAddress获取用户的地址。但是,如果您位于负载均衡器或内容分发网络后面,那么您可能需要首先尝试查看 Request.Headers["X-Forwarded-For"] - 这是用户原始 IP 地址所在的位置通常在请求被转发时写入。

我通常采取的方法是尝试以下方法:

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

在我从事的最后一个项目中,我们实际上记录了这两个项目,以防转发的标头被伪造。

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.

野味少女 2024-12-04 03:13:26

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

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

〆凄凉。 2024-12-04 03:13:26

您无法获取最终用户计算机的 MAC 地址。

您可以使用 Request.UserHostAddress 获取用户的公共 IP 地址。

请注意,每个用户的 IP 地址不是唯一的。
如果多个用户位于同一代理后面或位于公司网络上,他们通常会共享相同的地址。
您可以检查 X-Forwarded-For 标头获取更多信息。
请注意,此标头可以被链接或伪造。

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.

A君 2024-12-04 03:13:26
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";
            }
        }
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";
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文