locked
Get IP Address from user RRS feed

  • Question

  • User1044989675 posted

    How do I get the ip address from the logged in user? currently it is displaying ::1, and I have 

    Request.UserHostAddress;

    I want the IP address to display on a label

    Wednesday, March 13, 2013 12:39 PM

Answers

  • User-1965857832 posted

    Try this

                string clientIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (!string.IsNullOrEmpty(clientIp))
                {
                    string[] forwardedIps = clientIp.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    clientIp = forwardedIps[forwardedIps.Length - 1];
                }
                else
                {
                    clientIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }

    Please test it by accessing ur site from different machine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 13, 2013 2:43 PM

All replies

  • User-1965857832 posted

    Try this

    string clientIp = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
     if( !string.IsNullOrEmpty(clientIp) ) {
      string[] forwardedIps = clientIp.Split( new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries );
      clientIp = forwardedIps[forwardedIps.Length - 1];
     } else {
      clientIp = context.Request.ServerVariables["REMOTE_ADDR"];
     }
    Wednesday, March 13, 2013 12:45 PM
  • User1044989675 posted

    it says "context" does not exist in the current context

    Wednesday, March 13, 2013 12:50 PM
  • User-1965857832 posted

    Try this

                string clientIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (!string.IsNullOrEmpty(clientIp))
                {
                    string[] forwardedIps = clientIp.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    clientIp = forwardedIps[forwardedIps.Length - 1];
                }
                else
                {
                    clientIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }

    Please test it by accessing ur site from different machine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 13, 2013 2:43 PM
  • User220175380 posted

    Hi,

    Use this,

    IPAddress = HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")

    Thursday, March 14, 2013 3:17 AM