如何使用 C# 获取 Active Directory 中所有域的列表

发布于 2024-07-09 23:47:43 字数 88 浏览 5 评论 0原文

谁能帮我获取 Active Directory 中的所有域。 我已经尝试了很多次,但所有程序都只列出当前的工作域。

我怎样才能做到这一点?

Can anyone please help me to get all the domains in Active Directory. I have tried many times, but all the programs are listing only the current working domain.

How can I do this?

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

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

发布评论

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

评论(4

情深已缘浅 2024-07-16 23:47:43
Domain domain = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, "yourDomain", "username", "password"));

Forest forest = domain.Forest;

DomainCollection domains = forest.Domains;

上面使用了 System.DirectoryServices.ActiveDirectory 命名空间。 它将为您提供一个域集合,其中包含与给定域位于同一林中的所有域。

Domain domain = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, "yourDomain", "username", "password"));

Forest forest = domain.Forest;

DomainCollection domains = forest.Domains;

The above uses the System.DirectoryServices.ActiveDirectory namespace. It'll give you a domain collection containing all the domains that are in the same forest as your given domain.

别在捏我脸啦 2024-07-16 23:47:43

在让 LeeMobile 的代码 在我的案例中工作时遇到一些问题,因为它在运行森林时试图找到我的应用程序的当前域上下文.域名。 我可以通过做这样的事情来解决这个问题。

Forest forest = Forest.GetForest(new DirectoryContext(DirectoryContextType.Forest, "yourForestDomain", "username", "password"));
DomainCollection domains = forest.Domains;

I had some issues getting LeeMobile's code to work in my case bacause it was trying to find my application's current domain context while running forest.Domains. I was able to get around it by doing something like this.

Forest forest = Forest.GetForest(new DirectoryContext(DirectoryContextType.Forest, "yourForestDomain", "username", "password"));
DomainCollection domains = forest.Domains;
无声无音无过去 2024-07-16 23:47:43

使用 DirectorySearcher,您可以连接并读取一个 Active Directory 的结构,包括结构(组织单位、组、用户、计算机、域控制器)。 为了连接到不同的域,您需要该其他域的凭据。 我们在从属于与目标域不同的域的计算机连接到另一个域时遇到问题。 我也很好奇这是否可能。

Using DirectorySearcher you can connect and read the structure of one Active Directory, including the structure (organization units, groups, users, computers, domain controllers). In order to connect to a different domain, you would need credentials of that other domain. We had problems in connecting to another domain from a machine that belongs to a different domain than the target one. I'm also curious if that's even possible.

想念有你 2024-07-16 23:47:43

您还可以使用 System.DirectoryServices.ActiveDirectory.Forest.GetCurrentForest().Domains

var domains = Forest.GetCurrentForest().Domains.Cast<Domain>();
foreach (var domain in domains)
{
    Console.WriteLine(domain.Name);
}

You could also use System.DirectoryServices.ActiveDirectory.Forest.GetCurrentForest().Domains

var domains = Forest.GetCurrentForest().Domains.Cast<Domain>();
foreach (var domain in domains)
{
    Console.WriteLine(domain.Name);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文