关于获取客户端Mac地址

private static string GetClientMAC()
        {
            string mac_dest = string.Empty;
            try
            {
                string strClientIP = HttpContext.Current.Request.UserHostAddress.ToString().Trim();
                Int32 ldest = inet_addr(strClientIP); //目的地的ip 
                Int32 lhost = inet_addr("");   //本地服务器的ip 
                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 ex)
            {
                Log.Error("GetClientMAC", ex);
                //throw ex;
            }
            return mac_dest;
        }

 

上面的代码只能在局域网中使用,在广域网中是拿不到mac的(00-00-00-00-00-00),而且运行时间较长。

posted @ 2018-01-25 10:34  Shikyoh  阅读(449)  评论(0编辑  收藏  举报