【问题标题】:IP address of the user who is browsing my website正在浏览我的网站的用户的 IP 地址
【发布时间】:2012-11-27 17:01:06
【问题描述】:

我想知道客户端机器的IP地址,即浏览我网站的用户的IP地址。我正在尝试以下代码,但它正在返回服务器地址 -

public string GetClientIP()
{
    string result = string.Empty;
    string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (!string.IsNullOrEmpty(ip))
    {
        string[] ipRange = ip.Split(',');
        int le = ipRange.Length - 1;
        result = ipRange[0];
    }
    else
    {
        result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }

    return result;
}

如何找到正确的 IP 地址?

【问题讨论】:

  • 您可能会在此处遇到一些安全问题。你到底在找什么?客户端PC的IP?还是使用的可见 IP?

标签: c# http ip-address


【解决方案1】:
string myIP = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]).Split(',')[0].Trim();

注意:用于获取公共 IP 地址。

【讨论】:

    【解决方案2】:

    您可以使用“HTTP_X_FORWARDED_FOR”或“REMOTE_ADDR”标头属性。

    参考来自 developersnote.com blog 的 GetVisitorIPAddress 方法。

         /// <summary>
        /// method to get Client ip address
        /// </summary>
        /// <param name="GetLan"> set to true if want to get local(LAN) Connected ip address</param>
        /// <returns></returns>
        public static string GetVisitorIPAddress(bool GetLan = false)
        {
            string visitorIPAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    
            if (String.IsNullOrEmpty(visitorIPAddress))
                visitorIPAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    
            if (string.IsNullOrEmpty(visitorIPAddress))
                visitorIPAddress = HttpContext.Current.Request.UserHostAddress;
    
            if (string.IsNullOrEmpty(visitorIPAddress) || visitorIPAddress.Trim() == "::1")
            {
                GetLan = true;
                visitorIPAddress = string.Empty;
            }
    
            if (GetLan)
            {
                if (string.IsNullOrEmpty(visitorIPAddress))
                {
                    //This is for Local(LAN) Connected ID Address
                    string stringHostName = Dns.GetHostName();
                    //Get Ip Host Entry
                    IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);
                    //Get Ip Address From The Ip Host Entry Address List
                    IPAddress[] arrIpAddress = ipHostEntries.AddressList;
    
                    try
                    {
                        visitorIPAddress = arrIpAddress[arrIpAddress.Length - 2].ToString();
                    }
                    catch
                    {
                        try
                        {
                            visitorIPAddress = arrIpAddress[0].ToString();
                        }
                        catch
                        {
                            try
                            {
                                arrIpAddress = Dns.GetHostAddresses(stringHostName);
                                visitorIPAddress = arrIpAddress[0].ToString();
                            }
                            catch
                            {
                                visitorIPAddress = "127.0.0.1";
                            }
                        }
                    }
                }
            }
    
    
            return visitorIPAddress;
        }
    

    【讨论】:

      【解决方案3】:

      可以从请求中读取客户端IP:

      context.Request.ServerVariables["REMOTE_HOST"] 
      

      这里是获取不仅仅是客户端 IP 地址的代码:

      string browserInfo =
                   "RemoteUser=" + context.Request.ServerVariables["REMOTE_USER"] + ";\n"
                  + "RemoteHost=" + context.Request.ServerVariables["REMOTE_HOST"] + ";\n"
                  + "Type=" + context.Request.Browser.Type + ";\n"
                  + "Name=" + context.Request.Browser.Browser + ";\n"
                  + "Version=" + context.Request.Browser.Version + ";\n"
                  + "MajorVersion=" + context.Request.Browser.MajorVersion + ";\n"
                  + "MinorVersion=" + context.Request.Browser.MinorVersion + ";\n"
                  + "Platform=" + context.Request.Browser.Platform + ";\n"
                  + "SupportsCookies=" + context.Request.Browser.Cookies + ";\n"
                  + "SupportsJavaScript=" + context.Request.Browser.EcmaScriptVersion.ToString() + ";\n"
                  + "SupportsActiveXControls=" + context.Request.Browser.ActiveXControls + ";\n"
                  + "SupportsJavaScriptVersion=" + context.Request.Browser["JavaScriptVersion"] + "\n";
      

      【讨论】:

        【解决方案4】:
        string IPAddress = string.Empty;
        string SearchName = string.Empty;
        
        String strHostName = HttpContext.Current.Request.UserHostAddress.ToString();
        
        IPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
        

        【讨论】:

        • 你没有使用SearchName。并且IPAddress 不必在声明时设置为string.Empty(因为它在最后一次调用中被覆盖)
        • 它给出了我自己的地址。不是远程地址,我的意思是正在浏览我的网站的用户。
        • 当您托管您的应用程序服务器时,您可以在本地运行时获取客户端 IP 地址,您将获得的 IP 地址
        猜你喜欢
        • 1970-01-01
        • 2013-10-14
        • 2013-01-30
        • 1970-01-01
        • 1970-01-01
        • 2015-08-13
        • 1970-01-01
        • 2016-01-02
        • 2010-10-19
        相关资源
        最近更新 更多