asp.net获取访问者ip地址的函数

/// <summary>  
/// 获取IP地址  
/// </summary>  
     public static string IPAddress  
     {  
         get  
         {  
             string userIP;  
             // HttpRequest Request = HttpContext.Current.Request;  
             HttpRequest Request = HttpContext.Current.Request; // ForumContext.Current.Context.Request;  
             // 如果使用代理,获取真实IP  
             if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != "")  
                 userIP = Request.ServerVariables["REMOTE_ADDR"];  
             else  
                 userIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
             if (userIP == null || userIP == "")  
                 userIP = Request.UserHostAddress;  
             return userIP;  
         }  
     } 

  

posted @ 2016-08-06 02:50  Sunny Qiang  阅读(8451)  评论(0编辑  收藏  举报