I am seeking for a solution which allows me to get the currently working/(in use) ip address of my pc/laptop.
What I mean is I might be in LAN and then switch to WLAN. The ip addy will change and I need to stay up to date.
That is why I am not happy with approaches I found on the internet like IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
What is the best approach for this?
You can look up the active interfaces of the machine like this:
var interfaces = NetworkInterface.GetAllNetworkInterfaces().Where(a => a.OperationalStatus == OperationalStatus.Up);
var addresses = interfaces.First().GetIPProperties().UnicastAddresses.Where(a => a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
The Address property of any item in addresses holds the ip. From there you could monitor the adapters and decide when you need to switch over and what address to bind to.
Related
We are using maxmind geo ip location service in our web application. Utilizing this maxmind we need ipaddress to give as input.
We are following below snippet to get Ip address.
var ip = Request.UserHostAddress;
if (Request.Headers["X-Forwarded-For"] != null)
{
ip = Request.Headers["X-Forwarded-For"];
Console.WriteLine(ip + "|X-Forwarded-For");
}
else if (Request.Headers["REMOTE_ADDR"] != null)
{
ip = Request.Headers["REMOTE_ADDR"];
Console.WriteLine(ip + "|REMOTE_ADDR");
}
We are under corprate network, So in local environment as well as development environment we are used to get private ip address like this 10.X.X.X.
Using this address we always fails to locate the client location.
So real problem is when you passing external/public ip(190.X.X.X) to maxmind then only its working.
Do we really need to get external/public ips to give input to maxmind services.
You are using Maxmind for IP services that has everything to do with your clients' IP, not yours.
who are you trying to Geolocate?
For fraud services, who are you trying to evaluate?
Private IPs are used extensively by internal networks (home and corporations/enterprises..like yours), so they are not unique (aside from being non-routable on the public internet, etc.)
All geolocation providers work only on the public IP addresses. Private IP addresses are being used in LAN and can be re-issued again as long as it is not within the same LAN. There is no uniqueness to determine the geolocation information.
You are correctly using 10.xx.xx.xx range which is is reserved for private (internal) use.
All you need do is alter your code so that prior to your Maxmind look-up you replace the "10" IP to be looked-up with a "public" one for the same locale. If your company has multiple locations then you can create an array/table of internal IPs to public "locale identifying" IPs.
e.g. if the department in LA, USA uses 10.222.xx.xx; and NY,USA uses 10.123.xx.xx addresses then for these IPs simply Maxmind Lookup with an "equivalent" public IP. so for 10.123.xx.xx (NY, USA) look up 154.16.85.37 instead.
You may not even need to use Maxmind at all for your "10 IPs". E.g. if you simply need country code; then include it in your own "IP 10" look-up table/array and for IPs prefixed 10 instead of Maxmind look-up look-up from your own table.
I developed a program "installer" that will add and configure a new website on IIS.
My problem is that I want to check if the port given by the user is already used by another site before installing.
My project is in C # WinForm style.
Someone you have an idea?
string[] lPorts = System.IO.Ports.SerialPort.GetPortNames(); ?
Check this:
BindingInformation Property: Gets or sets the binding information for the current binding.
The value of this property is a colon-delimited string that includes the IP address, port, and host name of the binding. You can leave the host name blank. You can set the IP address to "*" to indicate that the binding works for all variables.
For example, a binding that is set for all IP addresses on port 80 and has no specified host name returns ":80:" from this property. A binding that is set for IP address 192.168.1.150 on port 8080 returns "192.168.1.150:8080:". A binding that is set for all IP addresses on port 80 for a host named "microsoft.com" returns ":80:microsoft.com".
The BindingInformation property values are maintained in the ApplicationHost.config file.
Also, you can check this: Get IIS bindings at runtime
foreach (Microsoft.Web.Administration.ConfigurationElement binding in mySite.GetCollection("bindings"))
{
string protocol = (string)binding["protocol"];
string bindingInfo = (string)binding["bindingInformation"];
if (protocol.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
string[] parts = bindingInfo.Split(':');
if (parts.Length == 3)
{
//Get the port in use HERE !!!
string port = parts[1];
yield return new KeyValuePair<string, string>(protocol, port);
}
}
}
You should look in the
Microsoft.Web.Administration namespace
You should use ServerManager.Sites to get a listing of sites. Take a look at the collection Bindings on each site. Each site may have one or more bindings, i.e. it may be accessed via one or more addresses / ports.
Your code above looks for the physical serial ports which where used before usb to connect modems and printers.
Hope this helps!
Not sure if you are looking for C#, but if you need powershell here you go..
Open PowerShell and enter the following:
import-module webadministration
get-webbinding
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?
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;
}
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).