Get User's IP Address

后端 未结 5 1675
半阙折子戏
半阙折子戏 2020-12-05 10:39

How can I get the current visitors IP address?

相关标签:
5条回答
  • 2020-12-05 11:16

    Edit: also found an interesting question regarding IP-related http headers here.

    Edit2: As mentioned in comments and in link I provided above, HTTP_X_FORWARDED_FOR header may contain multiple IP-addresses separated by comma. I didn't face this situation but suppose some corrections to my answer are required.

    I use this code to get the IP address (it returns IPAddress.None value if getting failed for some reason):

        /// <summary>
        /// Gets the IP address of the request.
        /// <remarks>
        /// This method is more useful than built in because in some cases it may show real user IP address even under proxy.
        /// <summary>
        /// Gets the IP address of the request.
        /// <remarks>
        /// This method is more useful than built in because in some cases it may show real user IP address even under proxy.
        /// The <see cref="System.Net.IPAddress.None" /> value will be returned if getting is failed.
        /// </remarks>
        /// </summary>
        /// <param name="request">The HTTP request object.</param>
        /// <returns></returns>
        public static IPAddress GetIp(this HttpRequest request)
        {
            string ipString;
            if (string.IsNullOrEmpty(request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
            {
                ipString = request.ServerVariables["REMOTE_ADDR"];
            }
            else
            {
                ipString = request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                    .FirstOrDefault();
            }
    
            IPAddress result;
            if (!IPAddress.TryParse(ipString, out result))
            {
                result = IPAddress.None;
            }
    
            return result;
        }
    
    0 讨论(0)
  • 2020-12-05 11:18

    Request.UserHostAddress

    0 讨论(0)
  • 2020-12-05 11:20

    HttpContext.Current.Request.UserHostAddress;

    or

    HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

    or

    HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    0 讨论(0)
  • 2020-12-05 11:21

    Try this to get external ip address of user..

    public static string getExternalIp()
        {
            try
            {
    
                string externalIP;
                externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
                externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
                             .Matches(externalIP)[0].ToString();
                return externalIP;
            }
            catch { return null; }
        }
    
    0 讨论(0)
  • 2020-12-05 11:33
    public String GetIP()
    {
        string ipString;
        if (string.IsNullOrEmpty(Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
        {
            ipString = Request.ServerVariables["REMOTE_ADDR"];
        }
        else
        {
            ipString = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
        }
        return ipString;
    }
    

    First trying to find out proxy IP,if its null we can get that system IP

    0 讨论(0)
提交回复
热议问题