1

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.

7
  • what code is returning the local host IP? Commented Oct 14, 2014 at 5:43
  • @RadioSpace like HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] is always null Commented Oct 14, 2014 at 5: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. Commented Oct 14, 2014 at 5: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... Commented Oct 14, 2014 at 6: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. Commented Oct 14, 2014 at 10:29

1 Answer 1

4
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

Sign up to request clarification or add additional context in comments.

4 Comments

I did that as mentioned in the updated code but it still giving me 127.0.0.1 am I missing something
Did you see the update? If my answer is wrong for your situation please say or accept the answer
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
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.