.Net获取IP地址

来自森大科技官方博客
http://www.cnsendblog.com/index.php/?p=292
GPS平台、网站建设、软件开发、系统运维,找森大网络科技!
http://cnsendnet.taobao.com

 

1.获取本机IP

2.获取网页客户端IP

    /// <summary>
        /// 获取IPv4地址
        /// </summary>
        /// <returns></returns>
        public static string GetIP4Address(HttpRequest request = null)
        {
            Func<IPAddress, bool> f = e => e.AddressFamily.ToString() == "InterNetwork";

            IPAddress ipa = Dns.GetHostAddresses((request ?? HttpContext.Current.Request).UserHostAddress).FirstOrDefault(f)
                ?? Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault(f);

            if (ipa != null)
            {
                return ipa.ToString();
            }

            return HttpContext.Current.Request.UserHostAddress;
        }

 

 

来自森大科技官方博客
http://www.cnsendblog.com/index.php/?p=292
GPS平台、网站建设、软件开发、系统运维,找森大网络科技!
http://cnsendnet.taobao.com

posted on 2020-01-13 18:03  森大科技  阅读(340)  评论(0编辑  收藏  举报

导航