.NET 中机器的域名?

发布于 2024-07-13 12:40:09 字数 335 浏览 4 评论 0原文

一定有一个简单的方法可以做到这一点,我不敢相信没有。 我扫描了网络,发现有 20 种不同的方法来查找当前用户所在的域,但没有一种方法可以获取当前计算机的域(或工作组)。

在非托管 C++ 中,这是通过以下方式检索的:

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

如果有办法在托管 C# 中本地获取相同的信息,有人可以帮助我吗?

编辑1:各位,请阅读问题。 我不是在寻找用户域名。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

多彩岁月 2024-07-20 12:40:10

System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain( ) 包裹 DsGetDcName 即使计算机不属于域,它也会在网络中搜索域控制器。 (请参阅备注

作为 NetGetJoinInformation 的替代方案,您可以将 GetComputerNameExCOMPUTER_NAME_FORMAT.ComputerNameDnsDomain 标志获取完整的 DNS 域名。

(如果不是域的一部分,它仍然返回 true,但结果字符串将为空。)

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

怼怹恏 2024-07-20 12:40:09

要获取正在运行程序的系统的当前域,您可以使用
System.DirectoryServices.ActiveDirectory.Domain

Domain domain = Domain.GetComputerDomain();
Console.WriteLine( domain.Name );

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 );
雪化雨蝶 2024-07-20 12:40:09

我从事的项目中用户可以在任何地方; 域计算机上的非域用户、非域计算机上的用户、未直接连接到第三方网络上的域的用户等。因此依赖 AD 已经是行不通的了。

在所有这些条件下,System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName 更加可靠。

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

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
攀登最高峰 2024-07-20 12:40:09

使用 GetCurrentDomainEnvironment.UserDomainName 相同,如果您的程序作为非域用户在域计算机上运行,​​则该方法无法正常工作。 我使用了以下代码:

try
{
    return System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain().Name;
}
catch (Exception)
{
    return Environment.UserDomainName;
}

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;
}
遮云壑 2024-07-20 12:40:09

系统.环境.用户域名

System.Environment.UserDomainName

沧桑㈠ 2024-07-20 12:40:09

如果你不想添加对System.DirectoryServices的依赖,你也可以调用 NetGetJoinInformation API。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文