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.
Related
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?
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.
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.
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;
I have the following code:
string ip = Request.ServerVariables["REMOTE_ADDR"];
Which, in the test environment does return the user IP addrress, but when we deploy the website to production, this variable has the IP of the server where the application is hosted. Any help?
My guess is that there is a proxy in the middle. Use HTTP_X_FORWARDED_FOR first, and if that's null, then use REMOTE_ADDR
From the MSDN article:
Although retrieving just the REMOTE_ADDR server variable should be enough, I found resources online that suggested that code like this should also check the HTTP_X_FORWARDED_FOR variable; if the request comes through a proxy server that translates the address, it's this variable that contains the correct address. If you request a server variable that doesn't exist, the ServerVariables property returns an empty string. Therefore, even though this property doesn't appear in my tests, attempting to retrieve its value doesn't cause trouble.
UPDATE:
If it's a load balancer that you have have settings changed on, you should ask to see if they can have the origination IP passed through. I know this can be done with Microsoft's ISA server.
If that's not an option, there are these other server variables that you can try and see if they produce a result:
"HTTP_X_COMING_FROM"
"HTTP_X_FORWARDED_FOR"
"HTTP_X_FORWARDED"
"HTTP_X_REAL_IP"
"HTTP_VIA"
"HTTP_COMING_FROM"
"HTTP_FORWARDED_FOR"
"HTTP_FORWARDED"
"HTTP_FROM"
"HTTP_PROXY_CONNECTION"
"CLIENT_IP"
"FORWARDED"
Why do you use old, VB-style server variables instead of Request.UserHostAddress?
See MSDN Library.
As the others have stated, you will get the IP address of the reverse proxy/SSL terminator if it doesn't make the requests look like they come from the original client (As is possible at least in ISA server, and probably in most other reverse proxies).
If not, you will get the public address of the client (which is probably a router address at the client site, as most LANs are NAT-ed).
How does your setup in the production environment differ from your test environment?
Are you actually getting the IP address of the Web server, or of some other server in the same network?
I see this is an old question but I ran across it and I think I know the answer. The answer is simpler than those above... I just ran into the same issue today. I bet you are trying to get the IP from a page being called by XMLHTTP which will return the server IP since it is the one making the request and not the user.