Get client's IP address and computer name? - c#

I have an ASP.Net 4.0 application, published on a company intranet network on an IIS 7.0 server, and I want to save the client's IP address in my database. So I want to get client's IP address and computer name.
I tried methods from internet searches but I get "SERVER IP" an "SERVER NAME". I think it's logical because all methods I tried is C# code that proceed server side.
So, I think I must use client side code like JavaScript.
Does anyone have the right method to do this?

You could use the UserHostAddress and UserHostName properties on the Request object:
string ip = Request.UserHostAddress;
string hostname = Request.UserHostName;

Related

Fail to get the IP address of server in ASP.NET

I needed to get the IP address of my Asp.Net server. I mean the IP of the machine that have the IIS when my Asp.Net application is running.
So I googled and found this: [Stackoverflow] Getting the IP address of server in ASP.NET?
And got this code:
var hostName = System.Net.Dns.GetHostName();
var address = System.Net.Dns.GetHostEntry(hostName).AddressList.First().ToString();
Good. It works perfectly, but only in development and test environment. In homologation environment the variable hostName gets correct value, but the address gets: "::1".
It's a super weirdo string. I can't even guess what is happening.
Can someone tell me what I'm doing wrong or what I'm not seen?

The ipaddress string is returning ::1. How can i get the full ipadrress of the user testing from localhost

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.
::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
edit
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.

System.Web.HttpContext.Current.Request.UserHostAddress;

I am using the following code to obtain the user I.P. address but something strange is happening. I am, receiving the same ip address every time no matter if I am on my desktop or on my iPhone. I am on our network where the web servers live when I hit the page from my desktop but on my iPhone I was not even in the building. I have not run into this issue before, the ip address that I am getting back is an internal one assigned to the web servers.
string ip = System.Web.HttpContext.Current.Request.UserHostAddress;
Response.Write(ip);
Outside of my network I still get the same ip address 172.16.0.22 which is a router address, not our external ip. Is there a way to obtain the external ip address.
see this link http://thepcspy.com/read/getting_the_real_ip_of_your_users/
// Look for a proxy address first
_ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
// If there is no proxy, get the standard remote address
if (_ip == null || _ip.ToLower() == "unknown")
_ip = Request.ServerVariables["REMOTE_ADDR"];
I hope this will be resolved by this time, I ran into same issue. Like you we I knew the IP address was one of the server on the network (Load Balancer).
If your solution is load balanced web servers, then there are high chances that your load balancer is updating the request header information, rerouting through some software load balancer or something.
In case on local testing, update the router configuration to pass the request details. read some router configuration manual, there should be something which will have this setting. :)
Supply your router information, someone will have router/hardware knowledge on this.
I am not a hardware expert, but ask your network administrator to either pass-on the header info as is or at least update "X-Forwarded-For" information. So you can build code to read this information consistently.
Find more information about the traffic routing techniques used in industry and issues here.
Sanjay
Try this;
if (!String.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"]))
ipAddress = HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"];
else
ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
Request.UserHostAddress return server IP which IIS run on.

Determining public IP in C# and comparing to a hostname

I have a program in C# that I want to get a news feed from a server I setup in my basement. I also want to setup this program so it can work locally. To do this I THINK I need to compare the resolved ip of my dyndns.biz hostname to my router's public ip (I have dynamic ip and a client on my server updating the ip of the hostname) and thus determine if the hostname needs to be used or the local ip of the server (192.168.0.100) or the hostname. I already have code to connect to the ftp server assuming I can get the right usage of the hostname versus the localized IP.
Edit: Anyways, in summary because I realized this might not look like a question, how can I determine a) the resolved IP of the hostname and b) the public IP of my router in a C# app
You're overcomplicating this.
Just open your hosts file (found in C:\Windows\System32\drivers\etc) and add your dyndns hostname routing it to loopback. That way you don't have to add any workaround code to your final application to prevent it from even asking your DNS or router:
127.0.0.1 yourhostname.dyndns.biz
To obtain the IP address of a hostname, use the following code:
IPAddress[] addresses = System.Net.Dns.GetHostAddresses("www.cnn.com");
To obtain your public IP address of your router or local network, you need to talk to an outside system that can tell you that part, and unfortunately I don't know if there is any such system that is free to use as well as easy to use from a program.

get remote Ip address or machine name

I have my GUI files published on a server... this server is where the IIS is running..
Now i access my GUI from a remote machine. how can i get this machines IP address or username.
I get the server name of the machine using this code:
string svrName = System.Net.Dns.GetHostName();
please help thanks...
maybe i was not clear enough:
Let me explain again..
there are 2 machines A and B.. A is where i have my published files for the GUI and also the IIS... the above code gives me the name of machine A
now i call the GUI from machine B. and i want the name of machine B
To get a remote user's IP from Asp.Net, you can use Request.ServerVariables["REMOTE_ADDR"] or Request.UserHostAddress.
The Request object should be available anywhere in your ASPX page.
I don't think it's possible to reliably get the hostname through ServerVariables.
Assuming I understand what you're asking, System.Web.HttpContext.Current.Request will give your server-side code lots of information about the client making the request.

Categories