How to programmatically check which domain I am connected to?

If I am connected to the local network here at work, I need my application to access our server using the internal IP address, otherwise I will need to use our external IP address when it is in the wild.

Currently, I'm just trying to connect through a local IP address, and then try an external one if it fails ... but the timeout takes too much time, and I was wondering if there is a way to find out in which area the computer is connected before how to try.

Edit: Patrick> Essentially, the application runs on a tablet PC that connects to the local network a couple of times a day. It is approximately equal to the number of network connections and the time that it connects locally.

All computers have a domain account when they are connected to the network (and have domain accounts with a naming convention of the type "LOCTabletx", where x is the number assigned to the machine when it is displayed.

What I'm looking for is a quick way to find out if a computer is connected to our local network or the Internet. Using Environment.UserDomainName gets a LOCTabletx, not a domain name.

EDIT

If this helps someone, I'm just trying to execute DNS. Allow the name of the machine that I can guarantee will be on the network (one of the servers). This works well enough for me.

+6
c # networking dns
source share
6 answers

You tried:

Environment.UserDomainName 

You can also look at the active IP addresses on the computer and request the 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 find out which network you are on, you can subscribe to System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged to process your computer networks when the application starts.

+7
source share
 System.Environment.UserDomainName 
+5
source share
 System.Security.Principal.WindowsIdentity.GetCurrent().AuthenticationType 

This will return NTLM if it is not connected to the network.

+4
source share

Another way, but I do not know if this is really better than other solutions:

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

this is a string that returns "Kerberos" in the active directory. Not sure what he will say if not connected to the domain.

+3
source share

You want to look at the Network Location Awareness API. Available in Windows Vista or later, it lets you programmatically find out which network you are connected to and receive notifications when this changes.

This may be familiar to you in the form "Is this a home / work / public network?" dialog window.

+2
source share

Environment.UserDomainName ... gives the name of the machine if you are not connected to the domain. It gives you a domain name if you are connected to a domain. If you take a machine connected to a domain from the network and exit “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 computer caches domain credentials for approximately 30 days.

If you log in to your computer account, you will receive the name of the machine.

0
source share

All Articles