Asp.net MVC獲取訪問系統的客戶端計算機的主機名和IP地址

追憶似水流年發表於2016-10-21
string HostName = string.Empty; 
string ip = string.Empty;
string ipv4 = String.Empty;

if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))
     ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
if (string.IsNullOrEmpty(ip))
     ip = Request.UserHostAddress;

 // 利用 Dns.GetHostEntry 方法,由獲取的 IPv6 位址反查 DNS 紀錄,
 // 再逐一判斷何者為 IPv4 協議,即可轉為 IPv4 位址。 foreach (IPAddress ipAddr in Dns.GetHostEntry(ip).AddressList) { if (ipAddr.AddressFamily.ToString() == "InterNetwork") { ipv4 = ipAddr.ToString(); } } HostName = "主機名: " + Dns.GetHostEntry(ip).HostName + " IP: " + ipv4;

  在本機進行程式程式碼除錯測試,其中字串ip會顯示為::1,是IPv6的IP地址格式,相當於127.0.0.1。最終處理成IPv4的顯示地址。

-------------------------------------------------------------------------------------------------------------------------------------------------

上述方法,程式碼除錯沒有問題,但是放在伺服器上後,客戶端訪問就出問題了,有部分電腦能獲取主機名,但是ipv4顯示為空,另外一部分

Dns.GetHostEntry(ip)中的ip正常,但是執行Dns.GetHostEntry(ip)貌似為null,導致出現異常。最後選擇的方法是隻記錄客戶端的IPV4,

/// <summary>
        /// 獲取web客戶端ip
        /// </summary>
        /// <returns></returns>
        public static string GetWebClientIp()
        {

            string userIP = "未獲取使用者IP";

            try
            {
                if (System.Web.HttpContext.Current == null
                 || System.Web.HttpContext.Current.Request == null
                 || System.Web.HttpContext.Current.Request.ServerVariables == null)
                {
                    return "";
                }

                string CustomerIP = "";

                //CDN加速後取到的IP simone 090805
                CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
                if (!string.IsNullOrEmpty(CustomerIP))
                {
                    return CustomerIP;
                }

                CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

                if (!String.IsNullOrEmpty(CustomerIP))
                {
                    return CustomerIP;
                }

                if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
                {
                    CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                   
                    if (CustomerIP == null)
                    { 
                        CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 
                    }
                }
                else
                {
                    CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }

                if (string.Compare(CustomerIP, "unknown", true) == 0 || String.IsNullOrEmpty(CustomerIP))
                {
                    return System.Web.HttpContext.Current.Request.UserHostAddress;
                }
                return CustomerIP;
            }
            catch { }

            return userIP;

        }

 

相關文章