Ejemplo n.º 1
0
        public static string GetUserPublicHostAddress(this HttpRequestBase request)
        {
            var userHostAddress = request.UserHostAddress;

            // Attempt to parse.  If it fails, we catch below and return "0.0.0.0"
            // Could use TryParse instead, but I wanted to catch all exceptions
            IPAddress.Parse(userHostAddress);

            var xForwardedFor = request.ServerVariables["X_FORWARDED_FOR"];

            if (string.IsNullOrEmpty(xForwardedFor))
            {
                return(userHostAddress);
            }

            // Get a list of public ip addresses in the X_FORWARDED_FOR variable
            var publicForwardingIps = xForwardedFor.Split(',').Where(ip => !IPAddressHelper.IsPrivate(ip)).ToList();

            // If we found any, return the last one, otherwise return the user host address
            return(publicForwardingIps.Any() ? publicForwardingIps.Last() : userHostAddress);
        }