0

I'm creating a web application where I need a functionality that if someone client log in to my website the client's public IP address must be logged. I have some javascript to do this but some of my pal not agree to use a javascript library reason they don't trust it. Such as http://l2.io/ip.js?var=myip as mentioned here

So we need all of it as server side code. So i found few more code in this link but unfortunately I'm not getting the expected result (it only returns local hosts IP). Could anyone help me to solve this please.

I have tried the bellow code

string VisitorsIPAddr = string.Empty;
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
    VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
{
    VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress;
}

but this is not providing me the public IP. Do I need to deploy it or something.

Community
  • 1
  • 1
Rahul Chakrabarty
  • 2,149
  • 7
  • 39
  • 70
  • what code is returning the local host IP? – RadioSpace Oct 14 '14 at 05:43
  • @RadioSpace like `HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]` is always null – Rahul Chakrabarty Oct 14 '14 at 05:50
  • " I'm not getting the expected result (it only returns local hosts IP)" - you need to explain in what case it happens. Otherwise it is direct duplicate of question you've linked to. – Alexei Levenkov Oct 14 '14 at 05:53
  • @Riki - note that your comment about about lack of "x-forwarded-for" header is not exactly clarifying you problem. The fact that request did not go through proxy (or proxy did not care to set the header) does not mean much... – Alexei Levenkov Oct 14 '14 at 06:11
  • @AlexeiLevenkov I have added some code that I tried but the code only giving me 127.0.0.1. I also hosted the site to IIS. – Rahul Chakrabarty Oct 14 '14 at 10:29
  • Since you can't post when "only giving me 127.0.0.1" happens, I guess you trying to access you site from the same machine the site is deployed too - if it is the case than there are plenty answers like http://stackoverflow.com/questions/1932843/iis-request-userhostaddress-returning-ipv6-1-even-when-ipv6-disabled explaining this expected behavior. – Alexei Levenkov Oct 14 '14 at 15:03
  • @AlexeiLevenkov I was running the in dev system, there I got 127.0.0.1 but in Server I got arror message as (code same as above):: No such host is known UserName and Password Required. Details: No such host is known – Rahul Chakrabarty Oct 17 '14 at 10:41

1 Answers1

3
HttpContext.Current.Request.UserHostAddress

This should get the public IP address of a client

EDIT

So you're after the IP address of your own machine as the rest of the world sees it?? This only really matters if you're hosting in on lets say an Intranet where the clients will be on the same network and thats the only address you will get if you use HttpContext.Current.Request.UserHostAddress. You will need to use a lookup api or something in this case. But then you'd know the external IP of your network!

If this is to be hosted on the internet then HttpContext.Current.Request.UserHostAddress will work just fine as every client will be displaying it's external IP.

It's showing 127.0.0.1 I'm guessing because you're testing on your local machine and this will be it's loopback address.

Hope this helps

matt_lethargic
  • 2,706
  • 1
  • 18
  • 33
  • I did that as mentioned in the updated code but it still giving me 127.0.0.1 am I missing something – Rahul Chakrabarty Oct 14 '14 at 10:33
  • Did you see the update? If my answer is wrong for your situation please say or accept the answer – matt_lethargic Oct 17 '14 at 15:20
  • This isn't working for me. I have this solution deployed to a server and I'm getting local ip addresses like 192.168.1.12 and 10.0.0.22 – DevShadow Apr 05 '17 at 17:10
  • They aren't local addresses they are private network addresses that are reserved for private networks. Try it again and run ipconfig from the pc you're trying it from and they should match – matt_lethargic Apr 05 '17 at 17:38