简体   繁体   English

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

[英]How to get list of all domains in Active Directory using C#

Can anyone please help me to get all the domains in Active Directory.任何人都可以帮助我获取 Active Directory 中的所有域。 I have tried many times, but all the programs are listing only the current working domain.我试了很多次,但所有的程序都只列出了当前的工作域。

How can I do this?我怎样才能做到这一点?

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.以上使用 System.DirectoryServices.ActiveDirectory 命名空间。 It'll give you a domain collection containing all the domains that are in the same forest as your given domain.它将为您提供一个域集合,其中包含与您的给定域位于同一林中的所有域。

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.我在让LeeMobile 的代码在我的案例中工作时遇到了一些问题,因为它在运行 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;

Using DirectorySearcher you can connect and read the structure of one Active Directory, including the structure (organization units, groups, users, computers, domain controllers).使用 DirectorySearcher,您可以连接并读取一个 Active Directory 的结构,包括结构(组织单位、组、用户、计算机、域控制器)。 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.我也很好奇这是否可能。

You could also use System.DirectoryServices.ActiveDirectory.Forest.GetCurrentForest().Domains您还可以使用 System.DirectoryServices.ActiveDirectory.Forest.GetCurrentForest().Domains

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C#获取Active Directory中的部门列表 - How to get list of Departments in Active directory using C# 如何通过 c# 中的 LDAP SSL 从 Active Directory 中的林中获取域 - How to get Domains from a forest from Active Directory over LDAP SSL in c# 如何枚举C#中每个森林的Active Directory域? - How to enumerate per-Forest Active Directory domains in C#? 如何使用C#更好地查询Active Directory中的多个域? - How can I better query multiple domains in Active Directory using C#? 如何使用 C# .NET 跨域设置/更改 Active Directory 用户密码? - How to set/change Active Directory user password across domains using C# .NET? C#:如何使用DirectoryContext,Domains和DirectoryEntry类在启用SSL的情况下连接到Active Directory? - C#: How to Connect to Active Directory with SSL Enabled using DirectoryContext, Domains and DirectoryEntry classes? 使用具有层次结构的c#从活动目录获取OU列表 - Get OU list from active directory using c# with hierarchy 如何从 C# 中的 Active Directory 用户获取所有属性 - How to get All attributes from an Active Directory user in C# 如何在C#中获取Active Directory的类列表 - How to get list of classes of an Active Directory in C# 如何使用C#获取Active Directory中没有内存泄漏的用户列表 - How to get list of user in Active Directory without memory leak using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM