Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取mac地址_C#_Asp.net_Network Programming_Ip_Mac Address - Fatal编程技术网

C# 如何获取mac地址

C# 如何获取mac地址,c#,asp.net,network-programming,ip,mac-address,C#,Asp.net,Network Programming,Ip,Mac Address,我可以获得连接到我的站点的MAC地址吗 此代码获取mac地址主机并返回错误权限 String macadress = string.Empty; foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { OperationalStatus ot = nic.OperationalStatus; if (nic.O

我可以获得连接到我的站点的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地址用户

二,。
如何获取ip用户???

您无法从请求中获取MAC地址,但是,您可以通过
请求获取ip。UserHostAddress
您无法获取最终用户机器的MAC地址

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

请注意,IP地址不是每个用户唯一的。
如果多个用户在同一个代理后面或在公司网络上,他们通常会共享同一地址。
您可以查看以获取更多信息。

请注意,此标头可以链接或伪造。

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

您可以尝试从
Request.UserHostAddress
获取用户地址。但是,如果您在负载均衡器或内容分发网络后面,那么您可能希望首先尝试查找
请求头[“X-Forwarded-For”]
——这是当请求被某个东西转发时,用户原始IP地址经常被写入的位置

我通常会采取以下方法:

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

在我参与的上一个项目中,我们实际上记录了这两个项目,以防forwarded for报头被伪造。

MAC地址是局域网的一项功能-您在数据包中看到的MAC地址将来自您的路由器,因为这是您局域网中将数据包转发给您的东西。
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";
            }
        }