首页 新闻 会员 周边

MVC如何获取浏览器IP地址

0
悬赏园豆:20 [已解决问题] 解决于 2015-09-08 15:50

之前采用百度上的获取地址的方式,始终是127.0.0.1

    public string GetBrowserIP()
        {
            string result = HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(result))
            {
                result = HttpContext.Request.ServerVariables["REMOTE_ADDR"];
            }
            if (string.IsNullOrEmpty(result))
            {
                result = HttpContext.Request.UserHostAddress;
            }
            if (string.IsNullOrEmpty(result))
            {
                return "0.0.0.0";
            }
            return result;
        }
View Code

哪位高手指导一下

jungan的主页 jungan | 初学一级 | 园豆:47
提问于:2015-09-08 15:15
< >
分享
最佳答案
1

你在级本机肯定是一直127吧?

收获园豆:20
顾晓北 | 专家六级 |园豆:10844 | 2015-09-08 15:17

部署到服务器一样的

jungan | 园豆:47 (初学一级) | 2015-09-08 15:20

@jungan: 部署到服务器以后你在哪儿访问的?

顾晓北 | 园豆:10844 (专家六级) | 2015-09-08 15:24

@顾晓北: 非服务器上。

jungan | 园豆:47 (初学一级) | 2015-09-08 15:25

@jungan: 

string result = String.Empty;
            try
            {
                serverVariables = serverVariables ?? System.Web.HttpContext.Current.Request.ServerVariables;
                result = serverVariables["HTTP_X_FORWARDED_FOR"];

                if (result != null && result != String.Empty)
                {
                    //可能有代理 
                    if (result.IndexOf(".") == -1)    //没有“.”肯定是非IPv4格式 
                        result = null;
                    else
                    {
                        if (result.IndexOf(",") != -1)
                        {
                            //有“,”,估计多个代理。取第一个不是内网的IP。 
                            result = result.Replace(" ", "").Replace("'", "");
                            string[] temparyip = result.Split(",;".ToCharArray());
                            for (int i = 0; i < temparyip.Length; i++)
                            {
                                if (IPHelper.IsIPAddress(temparyip[i])
                                    && temparyip[i].Substring(0, 3) != "10."
                                    && temparyip[i].Substring(0, 7) != "192.168"
                                    && temparyip[i].Substring(0, 7) != "172.16.")
                                {
                                    return temparyip[i];     
                                }
                            }
                        }
                        else if (IPHelper.IsIPAddress(result)) 
                            return result;
                        else
                            result = null;   
                    }

                }

                string IpAddress = (serverVariables["HTTP_X_FORWARDED_FOR"] != null
                    && serverVariables["HTTP_X_FORWARDED_FOR"] != String.Empty)
                    ? serverVariables["HTTP_X_FORWARDED_FOR"]
                    : serverVariables["REMOTE_ADDR"];

                if (null == result || result == String.Empty)
                    result = serverVariables["REMOTE_ADDR"];
            }
            catch (Exception)
            {

            }

            return result;

你试试。。

顾晓北 | 园豆:10844 (专家六级) | 2015-09-08 15:29

@顾晓北: 好的  感谢  我试下

jungan | 园豆:47 (初学一级) | 2015-09-08 15:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册