If I'm connected to the local LAN here at work, I need to have my app access our server via an internal IP, otherwise, I'll need to use our external IP when out in the wild.
Currently, I just try to connect via the local IP and then try the external if it fails... but the timeout takes a bit too long and I was wondering if there's a way to find out what domain the machine is connected to before trying.
Edit: Patrick> Essentially, the app runs on a tablet pc that is connected to the local network a couple of times a day. It's roughly equal between the number of times it connects over the network and the times that it connects locally.
All machines have a domain account when they are connected to the network (and have domain accounts with a naming convention of like "LOCTabletx" where x is a number given to the machine when it's ghosted.
What I'm looking for is a fast way to see if the machine is connected on our local network or the internet. Using Environment.UserDomainName gets me LOCTabletx and not the domain name.
EDIT
If it helps anyone, I just try to DNS Resolve the name of a machine that I can guarantee will be on the network (one of the servers). It works sufficiently well for me.
Have you tried:
Environment.UserDomainName
You could also take a look at the active IP addresses on the machine, and query for one that works on your local network...
var x = NetworkInterface.GetAllNetworkInterfaces()
.Where(ni => ni.OperationalStatus == OperationalStatus.Up)
.SelectMany(ni => ni.GetIPProperties().UnicastAddresses);
// do something with the collection here to determine if you're on the right network.
// just looping & printing here for example.
foreach (var item in x)
{
Console.WriteLine(item.Address);
}
And after you've figured out the network that you're on, you could subscribe to the System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged event to handle your computer jumping networks while your app is running.
System.Environment.UserDomainName
System.Security.Principal.WindowsIdentity.GetCurrent().AuthenticationType
This will return "NTLM" when not connected to the network.
You want to look at the Network Location Awareness API. Available on Windows Vista or later, it allows you to programmatically discover which network you're connected to, and be notified when this changes.
It might be familiar to you in the form of the "Is this a home / work / public network?" dialog box.
Another way, but I don't know if it is actually any better than the other solutions is:
System.Security.Principal.WindowsIdentity.GetCurrent().AuthenticationType
this is a string that returns "Kerberos" under active directory. Not sure what it would say when not connected to the domain though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With