コード例 #1
0
        public static string GetIPAddress([NotNull] this HttpRequest thisValue)
        {
            string ipStr = thisValue.Headers["HTTP_CF_CONNECTING_IP"];

            if (string.IsNullOrEmpty(ipStr))
            {
                ipStr = thisValue.ServerVariables["HTTP_X_CLUSTER_CLIENT_IP"];
            }
            if (string.IsNullOrEmpty(ipStr))
            {
                ipStr = thisValue.ServerVariables["HTTP_X_FORWARDED_FOR"];
            }
            if (string.IsNullOrEmpty(ipStr))
            {
                ipStr = thisValue.ServerVariables["REMOTE_ADDR"];
            }
            if (string.IsNullOrEmpty(ipStr))
            {
                ipStr = thisValue.UserHostAddress;
            }

            if (!string.IsNullOrEmpty(ipStr))
            {
                int n = ipStr.IndexOf(',');

                if (n > -1)
                {
                    ipStr = n > 0
                                                                ? ipStr.Substring(0, n)
                                                                : null;
                }
            }

            if (IPAddressHelper.IsLoopback(ipStr))
            {
                IPAddress ip = IPAddressHelper.GetLocalIP();
                if (ip != null)
                {
                    ipStr = ip.ToString();
                }
            }

            return(ipStr.ToNullIfEmpty());
        }