Machine's domain name in .NET?

前端 未结 6 1000
小蘑菇
小蘑菇 2021-02-03 22:47

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,

相关标签:
6条回答
  • 2021-02-03 22:57

    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;
    }
    
    0 讨论(0)
  • 2021-02-03 23:05

    System.Environment.UserDomainName

    0 讨论(0)
  • 2021-02-03 23:10

    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
    
    0 讨论(0)
  • 2021-02-03 23:12

    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.)

    0 讨论(0)
  • 2021-02-03 23:17

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

    0 讨论(0)
  • 2021-02-03 23:20

    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 );
    
    0 讨论(0)
提交回复
热议问题