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?
Related
I am trying to convert a VBScript COM component based Reverse/Forward IP checking system to C#.
This system was created to prevent the banning of SERPs like Googlebot using what was back then (becoming) the standard way of checking an IP was who it said it belonged to e.g a reverse/forward DNS check.
Although we have lists of SERP IP Ranges so we don't ban them if they come in - even with hack vectors - we cannot keep up with new ranges being added all the time.
The process is based around this short example.
It is explained simply here > http://ipadmin.junkemailfilter.com/rdns.php
This has been working fine for ages in VBScript but now I am converting to .NET I am having issues where people have set their IP to resolve to "localhost" like this one 113.168.154.182 as you just get back your own DNS server, Virgin media, or if I run it from my PC with c# I get my own computer name. The IP is from Vietnam > http://www.geoiptool.com/en/?ip=113.168.154.182
Now I am trying to use .NET and this code.
But as I am using this code to do get the hostname
IPHostEntry DNSHostIP = Dns.GetHostEntry("113.168.154.182");
hostname = DNSHostIP.HostName;
When I output the value of hostname I get my own computers name e.g std36w7.metal.mycompany.co.uk not localhost.
Then when I try and do a forward DNS check to get the list of IP addresses with this hostname I get my own IP addresses (one IPv6 one IPv4).
If I could get back "localhost" then I could have a check to skip it as a spoof along with anything starting with 10 or 192 etc.
However at the moment I cannot do this.
What is the best way of doing reverse/forward DNS checks which I thought was becoming the standard way of checking for spoofers nowadays in .NET?
And how can I handle people who have set (or some mistake might be causing it) their IP to be localhost.
Thanks
Simple. Your LOCAL DNS (guessing your router unless you have your own DNS server) is resolving it - upstream to it's INTERNET DNS server. Also if you really want it to return LOCALHOST, you'd have to literally edit your local HOSTS file and add an entry since no system ever returns the name LOCALHOST when you look up your ip even from a local DNS server. I believe the ONLY example is if you completely eliminate a DNS server to fallback on your local HOSTS file.
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 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.