I have tried fetching the ip from below mentioned methods
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] &
Request.UserHostAddress & Request.ServerVariables["REMOTE_ADDR"]
The problem is this that Request.ServerVariables["REMOTE_ADDR"] return Proxy ip and
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] can be tampered i want a foolproof method of fetching ip of client which cannot be tampered any help in this regard would be highly appreciated.
i want a foolproof method of fetching ip of client which cannot be tampered
Does not exist. Sorry.
The problem is this that Request.ServerVariables["REMOTE_ADDR"] return Proxy ip
Suppose the user is behind the corporate proxy. You'll be getting this proxy IP which for all intents and purposes is the closest thing to the user's IP. I mean if you got the user's final internal IP like 192.168.0.15 of what use would that be to you?
In addition to #Developer Art, consider the fact that many of the IP addresses on the internet are proxied from private class 'C' addresses. As an example, though my cable modem at home has a public IP address, my router provides a proxy for an internal Class C address (e.g. 192.168.1.123).
Finally... Given that 192. and 10. addresses are the norm for most consumer routers, the majority of your users would be coming through with an address that starts with one of those two numbers, and would not, by any measure, be unique.
Related
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.
Problem: there's an input field in an application where the user can enter either a host name or an IP address. I need to tell if the entered address corresponds to a real host.
I'm not talking about a simple regular expression check or an IPAddress.TryParse or Uri.CheckHostName. I don't have difficulty with checking a hostname: if it cannot be resolved to an IP address, then Dns.GetHostEntry will throw an exception. That's a piece of cake.
However. If I get an IP address input, then if I make a Dns.GetHostAddresses call it'll always succeed, even if I enter a stupid IP, like "1.1.1.1" ("1.1.1.1" is an IANA reserved IP address, our DNS server reports "non existent host/domain"). The Dns.GetHostAddresses immediately just spits back the IP I just passed in like everything would be all right whatsoever.
I cannot use the Dns.GetHostEntry either, because there are some IP addresses (like my virtual machines on the local network) which don't have any DNS host names associated with them, but they still have valid IP addresses, and Dns.GetHostEntry would throw exception to those (I guess it tries to resolve a hostname for them?).
I need a method call which actually tells me if it is a bogus IP or not, even if it looks like good IP address (by Uri.CheckHostName), but it doesn't have corresponding DNS host name.
The only reasonably check you can make is if IP is some sort of reserved IP. Otherwise there is not much you can do - even lack of "ping" (ICMP) responses and lack of responses on well-known ports (like HTTP - 80) means nothing.
Reserved as in:
127.0.0.0/8 - loopback (may or may not be considered "valid host")
224.0.0.0 - 239.255.255.255 - multicast (unlikely to be considered "valid host")
all zeros/all ones in subnet (i.e. 192.168.1.0 and 192.168.1.255 for 192.168.1.0/24 subnet) are all/broadcast - clearly not associated with particular how.
Check IP4 subnetting and linked RFCs for more info on special ranges/IPs.
Final edit for clarity - In my environment, DNS will only store one record per client. If a client has multiple NICs, or changes subnets, the original IP is registered in DNS until the corresponding DHCP record expires (this is an AD environment where DHCP registers DNS addresses).
In this scenario DNS has one, incorrect, record for the client. I want to query DHCP by client name, to see all IPs that are leased to it.
The only possible solution I have found is to dump all subnet info from DHCP (supported by the below API) then query against that, but that is not feasible in my environment, since multiple people would use this application, and I don't want the additional strain on DHCP.
I cannot change any configuration for DNS or DHCP.
Thanks,
This is similar to this question, but with the referenced API (here), I can only query via IP. Is it possible with this API, or any other, to query DHCP by hostname? (The issue being, DNS gives me an old IP for MachineA, I want to retrieve any other IPs being leased by MachineA from the DHCP server).
Edit: To clarify, I want to write a program that I can type in a hostname, it will then query a DHCP server for all IPs for that hostname in any subnet administered by that DHCP server. This is to workaround the issue of a machine with multiple NICs registering an IP that is useless to me (wireless), so for instance the DNS result may be NICA (wireless) but I want NICB (wired).
From what I can tell, you've encountered the age-old problem of which IP address to use. Now-a-days many computers have multiple NICs, some virtual, some local-only, some with internet access, etc... For the application to choose is very difficult. Most of the time I simply make the IP by which the application hosts things like sockets a configuration item--simply because the application is incapable of really choosing which is the right ip address to use. e.g. two NICs both with the same network access, which do you choose? If you run the application twice, maybe one should use NIC 1 and the other should use NIC 2--how would the app make that determination? (i.e. it can't).
Having said that, depending your needs, you can go looking for the best NIC and get it's IP address. For example, if you want an IPv4 address on a non-wireless NIC, you can do something like:
var ips = from ni
in NetworkInterface.GetAllNetworkInterfaces()
where ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet
from ip in ni.GetIPProperties().UnicastAddresses
where ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && ip.IsDnsEligible
select ip;
IPAddress address = ips.First().Address;
...error checking omitted for readability--apply whatever error checking suitable for your requirements.
You can even go so far as to check whether the address is link local (i.e. can communicate out of the local network segment--which usually means an address automatically assigned by Windows instead of DNS/DHCP) by seeing if the first two bytes of an IPv4 address are 169 and 254.
But, you need to specifically define what your requirements are. simply to say "undesirable wireless IP" doesn't provide unambiguous and verifiable criteria to tell what solution will always work for your needs.
If you are trying to locate a machine on the network, then querying DNS is probably what you want to do first. i.e. Think of a machine that has a static ip address on the network. It would register its name with the name service only, it would not show up in DHCP att all if the machine's IP stack is configured with the static address only.
I'm not sure how long it should take for a new machine or a recently changed IP address to show up in DNS. But if you want to see if DHCP has something different(newer), then query DHCP after trying it from DNS first.
Suppose there are 2 computers on same network, named com1 and com2.
On com1, if I call
Dns.GetHostEntry("com2")
surely enough, it returns only 1 ip address, like 192.168.1.2, which I could use it to communicate with com2. However, if I call
Dns.GetHostEntry("com1")
It will return all ip addresses (192.168.1.1(the one I want) as well as other addresses like 169.254.100.50 (vm address, I need to filter this out))
The question is, how can I get a unique "real" ip address for com1 in this case?
Thanks in advance.
After some research this question is actually hard to answer. If "com1“ has multiple NICs, it is hard to find which NIC it uses to communicate with "com2", I have found this SO link to get address on NICs that are connected to internet, VM address will not connect to internet. Obviously asking “com1" itself to find which NIC it uses to connect to "com2" is hard,should rather asking "com2" instead.
Ok then can you check the firewall settings of the "com2" or test it by turning it off.
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.