简体   繁体   中英

Getting IP Address of website client returning host IP

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM