Link to home
Start Free TrialLog in
Avatar of Carla Romere
Carla RomereFlag for United States of America

asked on

Client IP Address in Custom MasterPage on Intranet

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.
User generated imageThen 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.
User generated imageIt 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.
ASKER CERTIFIED SOLUTION
Avatar of svetaye
svetaye
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Carla Romere

ASKER

svetaye: I tried your code and I get the ipv6 address. I need to get te ipv4 address. Still working with it.
ivan_vagunin: I tried removing that last block of code as you suggested, and then no IP address shows up at all.
Avatar of Member_6283346
Member_6283346

Does System.Web.HttpContext.Current.Request.UserHostAddress contains correct hostname or client address?
Right now it's returning the ip address of the server and I need the ip address of the client.
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.
When you get the ipv6 address, this is the address of the server or the client machine?
The ipv6 address I assume is the address of the server because when I convert it to ipv4 it's the server.
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.
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great.
My final solution was slightly modified from the accepted best solution.