.NET 中机器的域名?
一定有一个简单的方法可以做到这一点,我不敢相信没有。 我扫描了网络,发现有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain( ) 包裹 DsGetDcName 即使计算机不属于域,它也会在网络中搜索域控制器。 (请参阅备注)
作为 NetGetJoinInformation 的替代方案,您可以将 GetComputerNameEx 与 COMPUTER_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.)
要获取正在运行程序的系统的当前域,您可以使用
System.DirectoryServices.ActiveDirectory.Domain。
To get the current domain of the system on which your progam is running you can use
System.DirectoryServices.ActiveDirectory.Domain.
我从事的项目中用户可以在任何地方; 域计算机上的非域用户、非域计算机上的用户、未直接连接到第三方网络上的域的用户等。因此依赖 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
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
使用 GetCurrentDomain 与 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:
系统.环境.用户域名
System.Environment.UserDomainName
如果你不想添加对System.DirectoryServices的依赖,你也可以调用 NetGetJoinInformation API。
If you don't want to add a dependency to System.DirectoryServices, you can also call the NetGetJoinInformation API directly.