locked
Trying to get IP Address fails in vs2017 when used to work in vs2010 when running locally RRS feed

  • Question

  • User2028823583 posted

    I have the following snippet of code that detects the IP address of the user logged into our company network.  he problem I'm having now, is when I run locally in vs2017, it fails on finding the MiddleIP.Value that I've put in bold below.    I need that middle number, to determine the users office location.   

    In vs2010, it returns a whole IP address   xx.xx.xx.xxx  when I run locally.  But in vs2017, it returns "::1" when I try to run locally.    Any suggestions/help on how I can fix this?  

    IPAddress.Value = Request.ServerVariables["REMOTE_ADDR"];
    String myIp = Request.ServerVariables["Remote_Addr"];
    String[] myIpArr = myIp.Split('.');
    MiddleIP.Value = myIpArr[1];
    String middleNumbers = myIpArr[1];

    Friday, January 4, 2019 1:34 PM

Answers

  • User283571144 posted

    Hi Susan,

    As far as I know,  while running the application on your machine locally i.e. in Visual Studio, the IP Address from the Request.ServerVariables REMOTE_ADDR will show as 127.0.0.1 or ::1.

    This happens because in such case the Client and Server both are the same machine. When you deploy it on a Server, you will see the results.

    I suggest you could firstly get the HTTP_X_FORWARDED_FOR to check the client machine’s which are behind Routers or Proxy Servers.

    If the IP Address is not found in the HTTP_X_FORWARDED_FOR server variable, it means that it is not using any Proxy Server, Then I suggest you could use REMOTE_ADDR server variable to get the IP address.

    Codes as below:

            public static String GetIP()
            {
                String ip =
                    HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    
                if (string.IsNullOrEmpty(ip))
                {
                    ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
    
                return ip;
            }

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 7, 2019 5:43 AM

All replies

  • User475983607 posted

    In vs2010, it returns a whole IP address   xx.xx.xx.xxx  when I run locally.  But in vs2017, it returns "::1" when I try to run locally.    Any suggestions/help on how I can fix this?  

    Correct and the expected results when running on localhost. 

    The IP address might not work as expected depending on the network configuration.  I recommend having a discussion with your IT department to make sure the design will work.  

    Friday, January 4, 2019 2:20 PM
  • User-943250815 posted

    As already said it is by design.
    On the other hand you can use "System.Web.Hosting.HostingEnvironment.IsDevelopmentEnvironment" to check is running on VS

    Sunday, January 6, 2019 12:06 AM
  • User283571144 posted

    Hi Susan,

    As far as I know,  while running the application on your machine locally i.e. in Visual Studio, the IP Address from the Request.ServerVariables REMOTE_ADDR will show as 127.0.0.1 or ::1.

    This happens because in such case the Client and Server both are the same machine. When you deploy it on a Server, you will see the results.

    I suggest you could firstly get the HTTP_X_FORWARDED_FOR to check the client machine’s which are behind Routers or Proxy Servers.

    If the IP Address is not found in the HTTP_X_FORWARDED_FOR server variable, it means that it is not using any Proxy Server, Then I suggest you could use REMOTE_ADDR server variable to get the IP address.

    Codes as below:

            public static String GetIP()
            {
                String ip =
                    HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    
                if (string.IsNullOrEmpty(ip))
                {
                    ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
    
                return ip;
            }

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 7, 2019 5:43 AM