using System; using System.Web; namespace CustPoWeb { /// <summary> /// MVC中获取客户端IPV4地址帮助类 /// LDH @ 2021-12-31 /// </summary> public static class IpHelper { /// <summary> /// 获取Web客户端IP地址 /// LDH @ 2021-12-31 /// </summary> /// <returns></returns> public static string GetWebClientIp() { var userIP = "未获取用户IP"; try { if (HttpContext.Current == null) return ""; var customerIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (!string.IsNullOrEmpty(customerIp)) return customerIp; if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null) { customerIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (customerIp == null) customerIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } else { customerIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } if (string.Compare(customerIp, "unknown", StringComparison.OrdinalIgnoreCase) == 0 || string.IsNullOrEmpty(customerIp)) return HttpContext.Current.Request.UserHostAddress; return customerIp; } catch { // ignored } return userIP; } } }
MVC中获取客户端IPV4地址帮助类
本文作者:Love In Winter
本文链接:https://www.cnblogs.com/LifeDecidesHappiness/p/15752655.html
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以扫一扫,任意打赏,您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/LifeDecidesHappiness/p/15752655.html
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以扫一扫,任意打赏,您的鼓励是博主的最大动力!
![扫一扫,支付宝打赏](https://files.cnblogs.com/files/LifeDecidesHappiness/%E6%94%AF%E4%BB%98%E5%AE%9D.bmp)
![扫一扫,微信打赏](https://files.cnblogs.com/files/LifeDecidesHappiness/%E5%BE%AE%E4%BF%A1%E6%94%B6%E6%AC%BE%E7%A0%81.bmp)