Reputation: 630
I want to retrieve the ip address of the user who has logged in using c#.
I have written the following code
var ipaddress = System.Web.HttpContext.Current.Request.UserHostAddress;
but the ipaddress
contains ::1
. How can i get the full address. I am just only testing the code in the localhost. I have iis7 installed.
Upvotes: 3
Views: 8377
Reputation: 3284
::1
according to the specs is actually a valid address pointing to loopback.
if you want to get the computers public ip address you'll have to use a domain name (with a DNS pointing back to the your local computer) or in the url use your public ip in place of localhost
your code is good (nothing to change there) however if you want to get your public ip addres (not ::1 or 127.0.0.1) you'll have to make the http call from the other interface (which means it will have to go out translate the DNS into a ip and query back). you won't be able to do that offline.
i hope this helps, sorry i can't be any clearer. this is more of a networking issue then programming.
Upvotes: 2