We help IT Professionals succeed at work.

Client IP Address in Custom MasterPage on Intranet

Carla Romere
Carla Romere used Ask the Experts™
on
I have successfully created a custom master page and deployed that on my local machine via deploy from VS2010. It works correctly, changes the master page for all subsites as I wanted. It displays my local ip address correctly as shown on the first screenshot.
Screenshot showing correct IP address.Then after much Googling, I managed to create a package from that solution and deploy it on the production server. It also, as expected, replaces all the master pages on the subsites as well. It does display an IP address, but it's the address of the server and not the IP address of the client accessing the server.
Screenshot showing the wrong address.It was created with a Control Template and the code for the ip address in that file is:
       protected void Page_Load(object sender, EventArgs e)
        {
            string IP4Address = String.Empty;

            foreach (IPAddress IPA in Dns.GetHostAddresses(System.Web.HttpContext.Current.Request.UserHostAddress))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            if (IP4Address != String.Empty)
            {
                //return IP4Address;
            }

            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            //return IP4Address;
            string ip = IP4Address;
            myip.Text = "My IP Address is: " + ip;
        }

Open in new window


I need help getting the client's IP address to display on the client's machine whenever they load our intranet SharePoint. We use this for support purposes and is absolutely critical that I get this working correctly.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Hello,

Please try this code instead yours:

protected void Page_Load(object sender, EventArgs e)
        {
              myip.Text = "My IP Address is: " +GetUserIP() ;
        }


private string GetUserIP()
{
return Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.ServerVariables["REMOTE_ADDR"];
}
Hi!
I think the correction code is following:
    protected void Page_Load(object sender, EventArgs e)
        {
            string IP4Address = String.Empty;

            foreach (IPAddress IPA in Dns.GetHostAddresses(System.Web.HttpContext.Current.Request.UserHostAddress))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            myip.Text = "My IP Address is: " + IP4Address ;
}

Note, that the last cycle in your code searches for IP of local machine, because Dns.GetHostName() return name of local machine:
            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }
So I guess what you need is just remove it.
Carla RomereDirector of Information Technology

Author

Commented:
svetaye: I tried your code and I get the ipv6 address. I need to get te ipv4 address. Still working with it.
Carla RomereDirector of Information Technology

Author

Commented:
ivan_vagunin: I tried removing that last block of code as you suggested, and then no IP address shows up at all.
Does System.Web.HttpContext.Current.Request.UserHostAddress contains correct hostname or client address?
Carla RomereDirector of Information Technology

Author

Commented:
Right now it's returning the ip address of the server and I need the ip address of the client.
Carla RomereDirector of Information Technology

Author

Commented:
svetaye - The 4guysfromrolla link is exactly the one I used to set this up to begin with. It does give me the ipv4 address, but it's the address of the server. I need the address of the client.

Commented:
When you get the ipv6 address, this is the address of the server or the client machine?
Carla RomereDirector of Information Technology

Author

Commented:
The ipv6 address I assume is the address of the server because when I convert it to ipv4 it's the server.

Commented:
Ok, so we still have the issue of obtaining the client's IP.
Unfortunately I can't check this on SharePoint server right now. I will check it later. I any case you always have a JavaScript option.
Carla RomereDirector of Information Technology

Author

Commented:
Okay - it's working now. It's only showing ipv6 on my local laptop, when I load it from another machine, it's getting the ipv4 address of the remote client. So I think I need to disable ipv6 on my local machine because we are not using it anywhere on our network yet.
Carla RomereDirector of Information Technology
Commented:
myip.Text = "My IP Address is: " + Request.ServerVariables["REMOTE_ADDR"];

That's all I had to use and it's correct.

Commented:
Great.
Carla RomereDirector of Information Technology

Author

Commented:
My final solution was slightly modified from the accepted best solution.