獲取遠程請求的IP地址、本機Mac地址和客戶端Mac地址


我在近期項目里面去記錄異常日志時,用到了這兩個地址,也是從網上和前輩那里學習到的,本人項目是MVC框架的,自己整理了一個公共方法類,包括獲取遠程客戶端IP和Mac地址,以及獲取本機Mac地址的方法,代碼如下:

 

 1 using System;
 2 using System.Management;
 3 using System.Runtime.InteropServices;
 4 using System.Web;
 5 
 6 namespace St_Te.Ultity
 7 {
 8     /// <summary>
 9     /// 獲取IP和Mac地址的方法類
10     /// </summary>
11     public class GetIPandMac
12     {
13         /// <summary>
14         /// 獲取客戶端IP地址
15         /// </summary>
16         public static string  GetIP()
17         {
18             return HttpContext.Current.Request.UserHostAddress;
19         }
20 
21         /// <summary>
22         /// 獲取本機Mac地址
23         /// </summary>
24         /// <returns></returns>
25         public static string GetMac()
26         {
27             ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
28 
29             ManagementObjectCollection mocollection = mc.GetInstances();
30 
31             foreach (ManagementObject mo in mocollection)
32             {
33                 if ((bool)mo["IPEnabled"] == true)
34                 {
35                     var mac = mo["MacAddress"].ToString();
36                     mo.Dispose();
37                     return mac;
38                 }
39             }
40             return "";
41         }
42 
43         
44         [DllImport("Iphlpapi.dll")]
45         private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
46         [DllImport("Ws2_32.dll")]
47         private static extern Int32 inet_addr(string ip);
48 
49         /// <summary>
50         /// 獲取客戶端Mac地址
51         /// </summary>
52         /// <returns></returns>
53         public static string GetClientMAC()
54         {
55             string mac = string.Empty;
56             // 在此處放置用戶代碼以初始化頁面
57             try
58             {
59                 if (HttpContext.Current.Request.UserHostAddress != null)
60                 {
61                     string strClientIP = HttpContext.Current.Request.UserHostAddress.Trim();
62                     int ldest = inet_addr(strClientIP); //目的地的ip 
63                     inet_addr("");
64                     long macinfo = new Int64();
65                     int len = 6;
66                     SendARP(ldest, 0, ref macinfo, ref len);
67                     string macSrc = macinfo.ToString("X");
68                     while (macSrc.Length < 12)
69                     {
70                         macSrc = macSrc.Insert(0, "0");
71                     }
72                     for (int i = 0; i < 11; i++)
73                     {
74                         if (0 == (i % 2))
75                         {
76                             if (i == 10)
77                             {
78                                 mac = mac.Insert(0, macSrc.Substring(i, 2));
79                             }
80                             else
81                             {
82                                 mac = "-" + mac.Insert(0, macSrc.Substring(i, 2));
83                             }
84 
85                         }
86                     }
87                 }
88             }
89             catch (Exception ex)
90             {
91                 throw ex;
92             }
93             return mac;
94         }
95     }
96 }
View Code

 

說明一下,獲取IP地址的方法,如果直接在controller里面獲取,則這樣寫:

var ip = Request.UserHostAddress;

 

獲取Mac地址的方法里引用了ManagementClass類,需要添加System.Management.dll的引用。

 

ManageMentClass 對象被實例化時,根據初始化參數的不同能夠獲取不同的本地信息:

"Win32_NetworkAdapterConfiguration" 獲取本機網絡適配器對象。據此可以獲取網絡地址等。

"Win32_DiskDiver" 獲取本機硬盤的相關信息,

"Win32_Processor" 獲取本機CPU相關的信息。

"Win32_OperatingSystem" 獲取內存等相關信息。

 /***************************我是可愛的分割線*******************************/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM