0

I have a website hosted through IIS and want to have an include . In that testfile.aspx I want to get the IP address of the client and redirect based off of requirements. However when I try to get the ip address I am given the ip of the machine my website is being hosted on and not the user

How to get a user's client IP address in ASP.NET?

I have tried all the methods in the link above and they all return the ip address of the host machine. Ive tried the code in the include page as well as the code behind of a sample test page and it still does not work.

Version 1

Dim ipAdd As String 
ipAdd = Request.ServerVariables("REMOTE_ADDR")
ClientScript.RegisterStartupScript(Me.GetType(), "myalert", "alert('This is your ip " & ipAdd & "');", True)

Version 2

ClientScript.RegisterStartupScript(Me.GetType(), "myalert", "alert('This is your ip " & Request.UserHostAddress & "');", True)

Both those methods return the public ip of my IIS web server

  • 1
    Generate some traffic, then check your IIS logs to see if the client IP address that is coming in is the one you are expecting to see. – John Wu Dec 12 '19 at 20:09
  • 1
    Ask if your provider uses [NAT](https://whatismyipaddress.com/nat). And if it can be disabled. – VDWWD Dec 12 '19 at 20:11
  • my log only shows the ip of the host machine, and we do use a NAT but it cannot be disabled which is fine I'm only interested in the public ip – justALostCoder Dec 12 '19 at 20:31
  • @JohnWu looking at my trace log file it lists the ip of the host machine as s-ip column – justALostCoder Dec 12 '19 at 23:03
  • Check to see if there is an [x-forwarded-for](https://support.stackpath.com/hc/en-us/articles/360021658292-Getting-Real-Client-IPs-with-X-Forwarded-For) header (e.g. by adding it to the IIS logs). If there is, you can use that. – John Wu Dec 12 '19 at 23:11
  • @JohnWu I've tried the code that gets the variable and it is null and instead fetches the REMOTE_ADDR variable and that is the host ip.. – justALostCoder Dec 12 '19 at 23:28
  • ive manually created an x-forwarded-for field in the log and all i see is a dash in the logs, guessing its not recognized. – justALostCoder Dec 13 '19 at 00:14
  • you could try :`public string getipaddress() { string ipaddress; ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (ipaddress == "" || ipaddress == null) ipaddress = Request.ServerVariables["REMOTE_ADDR"]; return ipaddress; }` – Jalpa Panchal Dec 13 '19 at 09:17
  • @JalpaPanchal I've tried that and I get the public ip of the host machine. – justALostCoder Dec 13 '19 at 16:21

2 Answers2

1

I use this in a class file to get the UserHostAddress, which is the IP address of the connecting client:

string sUserHostAddress = HttpContext.Current.Request.UserHostAddress;

I am working with WebForms and there I can use this to get it in the code behind file:

string sUserHostAddress = Request.UserHostAddress;

Hope this helps.

EDIT: I have put a page on my website where you can see the result. Check your IP address

KH S
  • 444
  • 1
  • 4
  • 8
  • hello KH S , trying either of those methods in my webforms project returns the ip of the host machine the website is on. – justALostCoder Dec 12 '19 at 22:59
  • Do you have the site running locally on your machine? That's when I get this too. If you use a different computer you should see a different IP address. On a webserver you will see the actual IP address of the client. The same is if you access the website using a webbrowser on the webserver. – KH S Dec 13 '19 at 09:33
  • when I test in VS i get local ip (127.0.0.1), my site is hosted via iis on web server. accessing through web browser on my desktop still returns public ip of host machine – justALostCoder Dec 13 '19 at 16:24
  • I have put a webpage on my site and edit the answer to get you there. This is what I would expect you see. Could you post some code of your implementation? – KH S Dec 13 '19 at 20:05
  • I've added the code that I am using. Both are being called on the Page_Load() function of the aspx code behind. – justALostCoder Dec 13 '19 at 21:24
  • You program in VB, right? I use C# so I am not 100% sure. I used your code and added the .Get as I got an error otherwise in VS: ipAdd = Request.ServerVariables.Get("REMOTE_ADDR"); I do get the client IP address locally and on the webserver. I don't get the ip of the host machine. – KH S Dec 13 '19 at 23:30
  • ive tried .Get and it still gives me the public ip of the server the site is hosted on. – justALostCoder Dec 13 '19 at 23:57
  • looks like the issue was a NAT policy put in place, something was changed and your code provided now works. Thank you for the help. Wish I could mark both the answers at correct. – justALostCoder Dec 16 '19 at 17:02
  • That's great. The answer you've marked actually solved the problem finally. No problem. – KH S Dec 16 '19 at 20:20
1

It seems the client IP address is being translated somewhere in the data path on its way to your application, for example by a proxy, NAT, or load balancer.

You need to figure out where and configure it to retain the original IP address in the x-forwarded-for header. Modern network appliances all have this option.

Without the original IP address being forwarded, it is not possible your code to retrieve it from any API. You can't read what you don't have.

John Wu
  • 50,556
  • 8
  • 44
  • 80
  • Thank you @John Wu , brought this issue up to Network Admin and they resolved the issue in the NAT policy and was able to use the code posted by KH S. – justALostCoder Dec 16 '19 at 17:00
  • sorry to come back after answering but nat policy was removed and now alot of other things arent working, so there needs to be a policy how could I modify it to have a x-forwarded-for property – justALostCoder Dec 20 '19 at 23:19
  • [Where can I ask questions for networking?](https://meta.stackexchange.com/questions/88303/where-can-i-ask-questions-for-networking) – John Wu Dec 20 '19 at 23:21