Asp.net获取Mac方法

  • A+
所属分类:ASP.NET MVC

Asp.net获取Mac方法

Controller

  1. using System.Runtime.InteropServices;  
  2. [DllImport("Iphlpapi.dll")]  
  3. private static extern int SendARP(Int32 dest, Int32 host, ref   Int64 mac, ref   Int32 length);  
  4. [DllImport("Ws2_32.dll")]  
  5. private static extern Int32 inet_addr(string ip);  
  6.         ///<summary>  
  7.         ///得到客户端ip  
  8.         ///</summary>  
  9.         ///<returns>ip</returns>  
  10.         private string GetClientIP()  
  11.         {  
  12.             string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
  13.             if (null == result || result == String.Empty)  
  14.             {  
  15.                 result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
  16.             }  
  17.             if (null == result || result == String.Empty)  
  18.             {  
  19.                 result = HttpContext.Current.Request.UserHostAddress;  
  20.             }  
  21.             return result;  
  22.         }  
  23.     ///<summary>  
  24.     /// 利用客户端ip得到客户端mac  
  25.     ///</summary>  
  26.     ///<param name="remoteip">客户端ip</param>  
  27.     ///<returns>int16类型的mac</returns>  
  28.     static private Int64 getremotemac(string remoteip)  
  29.     {  
  30.         Int32 ldest = inet_addr(remoteip);  
  31.         try  
  32.         {  
  33.             Int64 macinfo = new Int64();  
  34.             Int32 len = 6;  
  35.             int res = SendARP(ldest, 0, ref   macinfo, ref   len);  
  36.             return macinfo;  
  37.         }  
  38.         catch (Exception err)  
  39.         {  
  40.             Console.WriteLine("error:{0}", err.Message);  
  41.         }  
  42.         return 0;  
  43.     }  
  44.         ///<summary>  
  45.         /// int64类型的mac转换成正确的客户端mac  
  46.         ///</summary>  
  47.         ///<returns>mac</returns>  
  48.         private string GetClientMAC()  
  49.         {  
  50.             Int64 macid = getremotemac(GetClientIP());  
  51.             if (macid == 0)  
  52.                 return "0";  
  53.             string beforeMacAddr = Convert.ToString(macid, 16);  
  54.             string endMacAddr = "";  
  55.             string[] macArray = new string[6];  
  56.             for (int i = 0; i < 6; i++)  
  57.             {  
  58.                 macArray[i] = beforeMacAddr.Substring(i * 2, 2);  
  59.             }  
  60.             for (int i = 0; i < 6; i++)  
  61.             {  
  62.                 endMacAddr += macArray[5 - i] + "-";  
  63.             }  
  64.             endMacAddr = endMacAddr.Substring(0, endMacAddr.Length - 1);  
  65.             endMacAddr = endMacAddr.ToUpper();  
  66.             return endMacAddr;  
  67.         }  
钰玺

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: