[英]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.