31

There gotta be an easy way to do this, I can't believe there's none. I have scanned through net and found, like, 20 different methods to find in which domain current user is, but none to get domain (or workgroup) of current machine.

In unmanaged c++ this is retrieved by:

WKSTA_INFO_100 *buf;
NetWkstaGetInfo(NULL, 100, (LPBYTE*)buf);
domain_name = pBuf->wki100_langroup;

can someone help me, if there's a way to get same info in managed C# natively?

EDIT1: Folks, please read the question. I am NOT looking for user domain name.

galets
  • 17,802
  • 19
  • 72
  • 101
  • Hint: .NET Core doesn't have any obvious way to get this yet. I ended up P/Invoking NetWkstaGetInfo (which this question made easy to find). – Joshua Feb 01 '18 at 21:48

6 Answers6

43

To get the current domain of the system on which your progam is running you can use System.DirectoryServices.ActiveDirectory.Domain.

Domain domain = Domain.GetComputerDomain();
Console.WriteLine( domain.Name );
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • If a machine isn't in domain, we'll get an exception. – Shrike Mar 09 '11 at 11:47
  • @shrike - yes, you might actually want to check if domain is null before referencing the property on it. Kind of clutters the answer, though. – tvanfosson Mar 09 '11 at 13:05
  • 6
    Domain.GetCurrentDomain doesn't return null, it throws exception ActiveDirectoryOperationException ("Current security context is not associated with an Active Directory domain or forest."). So does Domain.GetComputerDomain() (which is better to use). But the latter throws different exception. Anyway using Domain's methods isn't safe. It's safer to use NetGetJoinInformation API (mentioned by @David). – Shrike Mar 09 '11 at 13:17
  • 1
    @Shrike -- as long as you know that it can throw an exception, it's not a problem. You just need to catch it and react appropriately. If you're using .NET, I think it's better to use the managed API in DirectoryServices. I've never actually used this method, but the namespace provides some very nice abstractions. – tvanfosson Mar 09 '11 at 14:08
  • According to Intellisense in .Net 4+ Domain.GetCurrentDomain() gets the domain object for the current user credentials in effect for the security context under which the application is running. To get the domain to which the computer belongs use Domain.GetComputerDomain(). EDIT: Just seen @Aleksandr's reply below which says the same thing – Redeemed1 Feb 12 '15 at 12:44
15

I work on a project where users could be anywhere; non-domain users on a domain machine, users on a non-domain machine, not directly connected to the domain on a third party network, etc. so depending on AD is already a non-starter.

System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName is far more reliable under all of these conditions.

http://blogs.msdn.com/b/trobbins/archive/2006/01/04/509347.aspx

https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipglobalproperties.domainname(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-2

Imports System.DirectoryServices
Imports System.Net.NetworkInformation

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            MsgBox("Domain: " & ActiveDirectory.Domain.GetComputerDomain.Name)
        Catch ex As Exception
            MsgBox(ex.GetType.ToString & ": " & ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            MsgBox("Domain: " & IPGlobalProperties.GetIPGlobalProperties().DomainName)
        Catch ex As Exception
            MsgBox(ex.GetType.ToString & ": " & ex.Message)
        End Try
    End Sub

End Class
jeepwran
  • 324
  • 2
  • 3
7

Using GetCurrentDomain is the same as Environment.UserDomainName, which works incorrectly if your program is running on a domain computer as a non-domain user. I've used the following code:

try
{
    return System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain().Name;
}
catch (Exception)
{
    return Environment.UserDomainName;
}
Aleksandr
  • 339
  • 3
  • 6
5

System.Environment.UserDomainName

bendewey
  • 39,709
  • 13
  • 100
  • 125
  • I'm running a .net application using IIS and this worked for me. – srmark Aug 14 '13 at 20:47
  • 6
    This isn't correct. The user domain name isn't necessarily the same as the machine name. In my workplace, the development machines are located in a subdomain. – Simon MᶜKenzie Nov 27 '14 at 04:30
  • This also isn't correct if the machine running your site is part of a server farm behind a reverse proxy. – SWalters Apr 23 '15 at 15:13
2

If you don't want to add a dependency to System.DirectoryServices, you can also call the NetGetJoinInformation API directly.

David
  • 34,223
  • 3
  • 62
  • 80
1

System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain() wraps DsGetDcName which will search the network for a domain controller even if the machine is not part of a domain. (see remarks)

As alternative to NetGetJoinInformation you could use GetComputerNameEx with the COMPUTER_NAME_FORMAT.ComputerNameDnsDomain flag to get the full DNS domain name.

(If not part of a domain, it still returns true, but the resulting string will be empty.)