Steven Evers
Steven Evers

Reputation: 17186

How do I programmatically check to see what domain I am connected to?

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.

Upvotes: 8

Views: 10229

Answers (6)

Les
Les

Reputation: 10595

System.Security.Principal.WindowsIdentity.GetCurrent().AuthenticationType 

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

Upvotes: 4

Les
Les

Reputation: 10595

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.

Upvotes: 0

Michael Todd
Michael Todd

Reputation: 17041

System.Environment.UserDomainName

Upvotes: 4

Kleinux
Kleinux

Reputation: 1541

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.

Upvotes: 3

Scott Ivey
Scott Ivey

Reputation: 41558

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.

Upvotes: 8

Roger Lipscombe
Roger Lipscombe

Reputation: 91805

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.

Upvotes: 3

Related Questions