Reputation: 1813
I am developing a web app that uses Asp.net. I found some code that I believe could get ip address of devices. The web app will only run on a network and the devices that are accessing are on the same network. So in other words, the server is local and devices do not need to be accessed through the internet, only on a local intranet.
The code I found is as follows:
string ipAdd = HttpContext.Current.Request.UserHostAddress;
When debugging, I found that the variable ipAdd
is set to '::1'. I believe I am getting that value because I am accessing the webserver through the same device. Will I be able to get the device's local IP with that piece of code, if I had a device access the server from another device, say an iPad?
Upvotes: 0
Views: 100
Reputation: 562
Yes, that has been my experience as well.
Localhost: http://localhost/GK/4.1/Account/AutoLogin.aspx
IP: ::1
Server: http://iis10/GK/Account/AutoLogin.aspx
IP: 192.168.1.70
(my PC's IP. running the app on my IIS 10 VM).
This article by Phil Haack is a little dated; but a good read (re-reading your question, your app will be hosted as an intranet, you might not run into this gotcha): A Gotcha Identifying the User's IP Address
I've used HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")
and reading Phil's article he pointed out:
The UserHostAddress property is simply a wrapper around the REMOTE_ADDR server variable which can be accessed like so.
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]
Upvotes: 1