Click here to Skip to main content
15,921,694 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everybody,

My system is in a network and my ip Address it is showing 192.168.1.37 (from the system). Now if I use the below code

C#
userid = Convert.ToInt64(SessionManager.LoggedInUserID);
        IPHostEntry host;
        string localIP = "?";
        host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily.ToString() == "InterNetwork")
            {
                localIP = ip.ToString();
            }
        }


I am getting the correct result in local, but in live it is taking the Server Ip address insted of client IP address.

But when I am using the below code

string localIP = Request.UserHostAddress;


It gives the result ip as 127.0.0.1 for the same system used in both cases. But I have not put it in live. I dont know whether it will work properly or not. But it (2nd one) is not working properly in local.

I need the first result for client system also.
Any help will be appreciated.
Posted
Updated 30-Nov-10 1:14am
v3

I just tried this and when working with cassini (the visual studio asp.net server) and had 127.0.0.1 as result.
When I put this on my local IIS with the site bound to 127.0.0.1 the
result was also 127.0.0.1.
When I bound the web site to my computer name E51EKGED which resolves to my true IP the result from the request from IE was finally the correct IP I (and you I presume) expected.
I'm now trying these scenarios with FF and Opera just to make sure
it wasn't some IE issue.

Modification:
Just dug a little deeper and found that it must be a security feature of the .NET framework:
The information sent by the browser is always the same (header info),
but the server side code doesn't allow the UserHostAddress to be read if the server is bound to 127.0.0.1.
Cassini (VS WebServer) can only listen to requests on 127.0.0.1 (localhost). This proves that to have user host address return the true client IP you'll have to deploy your solution to an IIS. This IIS as in my case may be on your developing machine, but the web site must be bound to some IP other than 127.0.0.1.
End Modification

Cheers


Manfred
 
Share this answer
 
v3
Comments
arindamrudra 30-Nov-10 7:55am    
You mean the second one will be the most effective.
arindamrudra 30-Nov-10 8:02am    
Cheers man its working fine. Thanks a lot for posting this answer.
Rajesh Anuhya 30-Nov-10 8:33am    
good call
arindamrudra 1-Dec-10 1:02am    
Thanks again for the modification.
Do you do any logging? If you do, add a debug entry where you want to check to user's host address. Then access your local website from another pc and then check the logs to see what gets logged for this.
However... Ideally, you would have staged environments for deployment testing, specifically to catch stuff like this. In a perfect world, you would have at minimum a development server, test server, pre-prod server, and then production. (If I'm missing the point please comment, and I'll see what I can do to help. :) )
 
Share this answer
 
Comments
arindamrudra 30-Nov-10 7:43am    
Yes you are absolutely right. But I need to fix it. Now I am going to push the second one in to live, lets see what happened?
arindamrudra 30-Nov-10 8:03am    
Thanks a lot for posting this answer. The second one is working fine in live.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900