其他分享
首页 > 其他分享> > .NET 获取当前IP

.NET 获取当前IP

作者:互联网

获取当前用户ip 

     public string getRemoteIp()
 {
       var ip = HttpContext.Request.Headers["X-Original-For"].FirstOrDefault();
      if (string.IsNullOrEmpty(ip))
         {
             ip = "*.*.*.*";
         }
        return ip;
}

OR

   /// <summary>
        /// 获取客户Ip
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string GetClientUserIp(this HttpContext context)
        {
            var ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrEmpty(ip))
            {
                ip = context.Connection.RemoteIpAddress.ToString();
            }
            return ip;
        }

但是我没有读取成功,很纳闷,先记录下来,后续解决,不只是浏览器还是什么缘故

标签:FirstOrDefault,string,ip,获取,context,IP,var,NET,HttpContext
来源: https://www.cnblogs.com/tiancaige/p/12532284.html