获取域名
我的计算机位于域(Active Directory)中,我需要动态获取域名。我在互联网上找到了以下代码:
SelectQuery query = new SelectQuery("Win32_ComputerSystem");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
foreach (ManagementObject mo in searcher.Get())
{
if ((bool)mo["partofdomain"])
{
this.Domain = mo["domain"].ToString();
break;
}
}
}
它完全按照我想要的方式工作,并完全按照我想要的方式返回域名(当我以管理员身份登录时)。如果用户不是域管理员,我会遇到拒绝访问
异常。
有人知道如何获得域,即使是非域管理员用户吗?
注意:我在 Internet System.Environment.UserDomainName;
上找到了此解决方案,但它只提供了域名的一部分。
即我的域名是:something.domain.com
UserDomainName
仅返回something
。
My computer is in a Domain (Active Directory) and I need to get the domain name dynamically. I found the following code on the internet:
SelectQuery query = new SelectQuery("Win32_ComputerSystem");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
foreach (ManagementObject mo in searcher.Get())
{
if ((bool)mo["partofdomain"])
{
this.Domain = mo["domain"].ToString();
break;
}
}
}
It works exactly as I want and returns exactly the domain name as I want (when I am logged as Administrator). If the user is not a Domain Admin, I have an Access denied
exception.
Does anybody know how to get the domain even with non-domain administrator users?
NOTE: I have found this solution on Internet System.Environment.UserDomainName;
but it only gives me a part of the domain name.
I.e. my domain is: something.domain.com
and the UserDomainName
returns only something
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为什么使用 WMI?您不能使用标准的.NET 功能吗?
Why are you using WMI? Can't you use the standard .NET functionality?
我将添加一个答案来尝试澄清一些事情,因为似乎存在一些混乱。主要问题是人们问了错误的问题,或者至少不够具体。
计算机的“域”实际上是什么意思?
当我们谈论计算机的“域”时,我们可能指的是几个东西。以下内容并非详尽的列表,但涵盖了最常见的情况:
我想要哪一个?
这很大程度上取决于您想要做什么。这个问题的原始发布者正在寻找计算机的“Active Directory 域”,这可能意味着他们正在寻找计算机的安全主体或用户的安全主体所属的域。通常,当您尝试以某种方式与 Active Directory 通信时,您需要这些。请注意,当前用户主体和当前计算机主体不一定位于同一域中。
Pieter van Ginkel 的答案实际上是为您提供本地网络堆栈的主 DNS 后缀(与
ipconfig /all
输出的顶部部分显示的内容相同)。在 99% 的情况下,这可能与计算机的安全主体和当前经过身份验证的用户的主体所属的域相同 - 但不一定。通常,当您尝试与 LAN 上的设备通信时,这就是您想要的,无论这些设备是否与 Active Directory 有关。对于许多应用程序来说,这仍然是与 Active Directory 对话的“足够好”的答案。最后一个选项是 DNS 名称,它比其他两个选项更加模糊和不明确。零到无穷大之间的任何 DNS 记录都可能解析为给定的 IP 地址 - 而且甚至不一定清楚您感兴趣的是哪个 IP 地址。 user2031519 的answer 指的是
HTTP_HOST
的值,该值在确定用户如何解析您的 HTTP 服务器以发送您当前正在处理的请求时特别有用。如果您尝试使用 Active Directory 执行任何操作,这几乎肯定不是您想要的。我如何获得它们?
当前用户安全主体的域
此一个很好又简单,这就是蒂姆的答案给你的。
当前计算机安全主体的域
这可能是 OP 想要的,对于这个,我们将不得不询问 Active Directory。
这将引发 < code>ActiveDirectoryObjectNotFoundException 如果本地计算机不是域的一部分,或者无法联系域控制器。
网络堆栈的主 DNS 后缀
这就是Pieter van Ginkel的答案给你的。它可能不完全是您想要的,但很有可能它对您来说足够好 - 如果不是,您可能已经知道原因了。
解析为计算机 IP 地址的 DNS 名称
这个问题很棘手,而且没有单一的答案。如果这就是您所追求的,请在下面发表评论,我将很乐意讨论您的用例并帮助您找出最佳解决方案(并在此过程中扩展此答案)。
I'm going to add an answer to try to clear up a few things here as there seems to be some confusion. The main issue is that people are asking the wrong question, or at least not being specific enough.
What does a computer's "domain" actually mean?
When we talk about a computer's "domain", there are several things that we might be referring to. What follows is not an exhaustive list, but it covers the most common cases:
Which one do I want?
This is highly dependent on what you are trying to do. The original poster of this question was looking for the computer's "Active Directory domain", which probably means they are looking for the domain to which either the computer's security principal or a user's security principal belongs. Generally you want these when you are trying to talk to Active Directory in some way. Note that the current user principal and the current computer principal are not necessarily in the same domain.
Pieter van Ginkel's answer is actually giving you the local network stack's primary DNS suffix (the same thing that's shown in the top section of the output of
ipconfig /all
). In the 99% case, this is probably the same as the domain to which both the computer's security principal and the currently authenticated user's principal belong - but not necessarily. Generally this is what you want when you are trying to talk to devices on the LAN, regardless of whether or not the devices are anything to do with Active Directory. For many applications, this will still be a "good enough" answer for talking to Active Directory.The last option, a DNS name, is a lot fuzzier and more ambiguous than the other two. Anywhere between zero and infinity DNS records may resolve to a given IP address - and it's not necessarily even clear which IP address you are interested in. user2031519's answer refers to the value of
HTTP_HOST
, which is specifically useful when determining how the user resolved your HTTP server in order to send the request you are currently processing. This is almost certainly not what you want if you are trying to do anything with Active Directory.How do I get them?
Domain of the current user security principal
This one's nice and simple, it's what Tim's answer is giving you.
Domain of the current computer security principal
This is probably what the OP wanted, for this one we're going to have to ask Active Directory about it.
This one will throw a
ActiveDirectoryObjectNotFoundException
if the local machine is not part of domain, or the domain controller cannot be contacted.Network stack's primary DNS suffix
This is what Pieter van Ginkel's answer is giving you. It's probably not exactly what you want, but there's a good chance it's good enough for you - if it isn't, you probably already know why.
DNS name that resolves to the computer's IP address
This one's tricky and there's no single answer to it. If this is what you are after, comment below and I will happily discuss your use-case and help you to work out the best solution (and expand on this answer in the process).
我是通过标题找到这个问题的。如果其他人正在寻找有关如何获取域名的答案,请使用以下环境变量。
我知道这个问题的作者提到了这一点,但我第一眼就错过了它,并认为其他人可能会这样做。
问题的描述要求的是完全限定域名 (FQDN)。
I found this question by the title. If anyone else is looking for the answer on how to just get the domain name, use the following environment variable.
I'm aware that the author to the question mentions this, but I missed it at the first glance and thought someone else might do the same.
What the description of the question then ask for is the fully qualified domain name (FQDN).
如果您希望特定用户能够访问全部或部分 WMI 对象空间,则需要按如下所示授予他们权限 此处。请注意,您必须以管理员身份运行才能执行此设置。
If you want specific users to have access to all or part of the WMI object space, you need to permission them as shown here. Note that you have to be running on as an admin to perform this setting.
我知道这已经很旧了。我只是想把这个转储到这里,供任何正在寻找域名答案的人使用。这与彼得的回答是一致的。正如里奇所说,“存在”一个错误。但是,您始终可以为此制定一个简单的解决方法。我可以通过 ping 域名来判断它们是否仍在域中。如果它响应,请继续执行我需要该域的任何操作。如果失败,我就会退出并进入“离线”模式。简单的字符串方法。
I know this is old. I just wanted to dump this here for anyone that was looking for an answer to getting a domain name. This is in coordination with Peter's answer. There "is" a bug as stated by Rich. But, you can always make a simple workaround for that. The way I can tell if they are still on the domain or not is by pinging the domain name. If it responds, continue on with whatever it was that I needed the domain for. If it fails, I drop out and go into "offline" mode. Simple string method.