views:

1044

answers:

6

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.

+5  A: 

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.

Scott Ivey
That only gets me my machine name on the domain. Not the domain name itself. For instance, if my comp is LOCProgrammer1 and the domain is Company.Local I get the former, and I'm looking for the latter.
SnOrfus
That's very odd. I get our domain name under both Vista and XP Pro. Perhaps you could use that as an indication that you're not logged into the domain?
Michael Todd
I get my local machine name - and I'm not logged into a domain. Makes sense though - it'll give you whichever system authenticated your login, either the local machine if you use a local user, or your domain if you log into a domain.
Scott Ivey
+4  A: 
System.Environment.UserDomainName
Michael Todd
"Missed it by _that_ much."
Michael Todd
+2  A: 

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.

Roger Lipscombe
Unfortunately the machines run WinXP Pro without an option to upgrade. I'll do some searching for an equivalent for WinXP though. Thank you.
SnOrfus
+2  A: 

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.

Kleinux
A: 

Environment.UserDomainName ...gives you the machine name if you are not joined to a domain. It gives you the domain name if you are joined to a domain. If you take a machine that is joined to a domain off the network and out "into the wild", Environment.UserDomainName will continue to provide the domain name even if you reboot and log back in (to your domain account). Your machine caches the domain credentials for about 30 days.

If you log in to your machine account, then you will get the machine name.

Les
+2  A: 
System.Security.Principal.WindowsIdentity.GetCurrent().AuthenticationType 

This will return "NTLM" when not connected to the network.

Les