I am trying to get the IP address of a user from ASP.NET MVC 5. I was looking for various examples, such as:
All of them gave the same result: the user is considered internal on the network. I had friends who tried their phones (which are not online). Here is my last attempt:
private static Logger _logger = LogManager.GetCurrentClassLogger(); public static bool IsIpInternal() { var ipAddress = HttpContext.Current.Request.UserHostAddress; var logEvent = new LogEventInfo(LogLevel.Info, _logger.Name, ipAddress); _logger.Log(logEvent); try { if (ipAddress != null) { var ipParts = ipAddress.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse).ToArray(); var isDebug = System.Diagnostics.Debugger.IsAttached; if (ipParts[0] == 10) { return true; } } } catch (Exception e) { logEvent = new LogEventInfo(LogLevel.Error, _logger.Name, e.Message); _logger.Log(logEvent); return false; } return false; }
The log displays 10.xxx.xx.xxx for all requests (based on the log). This is the internal address, not the IP address of the client connecting to the web application. IsIpInternal() always returns true. What am I doing wrong?
Please note that I ignore the addresses 192.168.xx and 172.16.xxx.xxx as internal.
source share