Get IPAddress from a hostname - c#

I want to get IP address of a host on my aspx page using C#, I'm using DNS class methods to get these.
It worked fine locally, but when I deployed the solution on IIS7 it returned only the IP address assigned by ISP but I want local IP address of that machine.
Any suggestion?

Here is Example for this.
In this example we can get IP address of our given host name.
string strHostName = "www.microsoft.com";
// Get DNS entry of specified host name
IPAddress[] addresses = Dns.GetHostEntry(strHostName).AddressList;
// The DNS entry may contains more than one IP addresses.
// Iterate them and display each along with the type of address (AddressFamily).
foreach (IPAddress address in addresses)
{
Response.Write(string.Format("{0} = {1} ({2})", strHostName, address, address.AddressFamily));
Response.Write("<br/><br/>");
}

I'm fairly confident that you can not get the local 192.168.C.D address of the local machine like this.
This is because of security and visibility (NAT's etc).
If you are looking to uniquely identify a user. I would look to sessions or cookies.

When looking up the ip address in a public DNS, you will get the officiall IP address communicated outwards. If NAT is used and you want the internal address you have to connect to a DNS server which holds the internal IP addresses.

you can use this method...
public static String GetIP()
{
String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if(string.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
return ip;
}

Related

Getting a client's IP address

I'm developing an ASP.NET project and I need to determine the client's IP address.
Currently, the following code on our production server
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
returns an IP address that is in the same subnet as the production server's IP address (i.e., not solving my problem as it's not
the client's IP address).
However, on our test server, that same line of code returns the client's IP address (the desired result). Anyone have an idea why?
I've tried using the above line of code subbing in the following values, all of which return null or an empty string:
"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"
As an alternative, I found the following code that does return an array of IP and contains the client's IP address (at index 4):
string strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
string ip = addr[4] != null ? addr[4].ToString() : "";
While debugging, I noticed that the IPAddress objects in addr have either (A) Address errors if it's an IPv6 address, or (B) ScopeId errors
if it's an IPv4 address. These errors don't seem to be an issue, but I'm not sure if they'll matter once in production.
Is there a better way to get the client's IP address than the way I have here?
Gets the HttpRequestBase object for the current HTTP request.
Request.UserHostAddress
you can get a client's IP address.
You can use this function to get ip address.
private string GetUserIP()
{
string ipList = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipList))
{
var ips= ipList.Split(',');
return ips[ips.Length - 1];
}
return Request.ServerVariables["REMOTE_ADDR"];
}
NOTE: HTTP_X_FORWARDED_FOR header may have multiple IP's. The correct IP is the last one.

how to find the ip address of client who is login to our website or browsing our website

Here i have a small problem, that i want to find out the IP Address of the client who is accessing my website, i tried so many but all these are giving me 127.0.0.1 as IP, while i am testing in local host,
Please some one provide the code snippet and help me,
Thanks in advance,
public string GetClientIP()
{
string result = string.Empty;
string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ip))
{
string[] ipRange = ip.Split(',');
int le = ipRange.Length - 1;
result = ipRange[0];
}
else
{
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
return result;
}
This is to be expected, your localhost IP address will most of the time be 127.0.0.1.
As said in the comments, when you will deploy your site and remote clients access it, their actual IP will correctly be retrieved.
If you want to try locally, you can try to configure your local network so that a remote computer on the same network access your web site. There you should see the IP address of that computer (for instance: 192.168.x.x).

Get server IP address

Is there a way to get the Sharepoint server's IP address?
This code below just retrieves the hostname but I need to know the IP address.
string hostName = SPServer.Local.Address;
Given a host name you can get the IP address of the host by using Dns.GetHostEntry in the System.Net namespace.
You can get the first IPv4 address of a host name using this code:
var ipAddress = Dns.GetHostEntry(hostName)
.AddressList
.First(ipAddress => ipAddress.AddressFamily == AddressFamily.InterNetwork);
The code will throw exceptions if either the host is unknown or if it doesn't have an IPv4 address.
SPServer has a member named 'SPServer.Address', if this isn't the value you are looking for then maybe try through HttpRequest.UserHostAccess

Getting and checking local machine's ip address

I am trying to get IP address on local machine:
private string GetIP()
{
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
foreach (IPAddress ipaddr in addr)
{
if (ipaddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
return ipaddr.ToString();
}
return "IP error";
}
However I can't find a way to identify which interface is the one i need. For example:
I am lucky that the one i need is second in the list. But if it were in the back i would get IP of a wrong interface. How to check if I am getting IP for local area connection (or in general, the interface responsible for the connection).
You may be able to enumerate the network interfaces directly (rather than just their IPs) and filter then based on their interface type:
var interfaces = NetworkInterface.GetAllNetworkInterfaces()
And then filter it with something like:
interfaces.Where(ni => ni.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel)
It may still return multiple network interfaces but it'll filter out at least some of them that you don't want. I use the above filter to get rid of loopback and virtual machine interfaces.
Then from there you can get the network interface's IP address using the IP properties.
In the spirit of brevity, once you determine which interface is the right one, you can get the IPv4 address (or at least one of them) of the interface using:
iface.GetIPProperties().UnicastAddresses.SingleOrDefault(ua => ua.Address.AddressFamily == AddressFamily.InterNetwork);
There is not method that return one address against the host name
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
it returns the local machine address for all the registered address in your DNS
So if in your DNS your machine has one name associated to one IP address it will return only that address otherwise it will return the list of addresses associated to that Host Name
you have to "filter" the list to understand what is your local address
Have a look at the below:
How to get the IP address of the server on which my C# application is running on?

How to get fqdn of ip addresses in c#?

On a machine there might be several IP addresses available. How can I find out all the belonging full qualified domain names (fqdn)?
Update:
I tried the following:
IPHostEntry he = Dns.GetHostEntry(Environment.UserDomainName);
foreach (IPAddress ipAddress in he.AddressList)
{
string x = ipAddress.ToString();
string y = Dns.GetHostEntry(ipAddress.ToString()).HostName;
}
I have a machine with 2 IP addresses, the ping using their fqdn returns the correct IPs. However, the code above always return me the one fqdn of the first IP.
My setup looks as follows:
IP1:
123.123.123.123
Name1
IP2:
456.456.456.456
Name2
Both ping and nslookup returns correct value.
The problem is that both rows
Dns.GetHostEntry("123.123.123.123").HostName;
Dns.GetHostEntry("456.456.456.456").HostName;
returns "Name1" (instead of "Name1" and "Name2").
However, the codes
Dns.GetHostEntry("Name1").HostName;
Dns.GetHostEntry("Name2").HostName;
work correctly.
You resolve each IP address to the netbios name.
Dim hostEntry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("192.168.115.54")
Console.WriteLine(hostEntry.HostName)
For example if I resolve my IP i get:
PC-MYNAME.MYDOMAIN.local
Then you can also use ActiveDirectory to enumerate the CurrentForrest (available domains).

Categories