读取/过滤分发组的活动目录的子组?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了读取/过滤分发组的活动目录的子组?相关的知识,希望对你有一定的参考价值。
我有一个域名为myDomain.local
的Active Directory,在它下面存在一个包含许多组的Distribution Group
。
如何(以编程方式)读取所有这些子组以检索其名称列表?
以及如何优化查询以过滤结果,以便它只检索以Region
结尾的所有组?
顺便说一下,我使用的是C#.Net,ASP.Net和sharepoint,而且我没有AD经验。
答案
如果您使用的是.NET 3.5(或者可以升级到它),则可以使用System.DirectoryServices.AccountManagement
命名空间来使用此代码:
// create the "context" in which to operate - your domain here,
// as the old-style NetBios domain, and the container where to operate in
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "cn=Distribution Group,dc=YourDomain,dc=local");
// define a "prototype" - an example of what you're searching for
// Here: just a simple GroupPrincipal - you want all groups
GroupPrincipal prototype = new GroupPrincipal(ctx);
// define a PrincipalSearcher to find those principals that match your prototype
PrincipalSearcher searcher = new PrincipalSearcher(prototype);
// define a list of strings to hold the group names
List<string> groupNames = new List<string>();
// iterate over the result of the .FindAll() call
foreach(var gp in searcher.FindAll())
{
// cast result to GroupPrincipal
GroupPrincipal group = gp as GroupPrincipal;
// if everything - grab the group's name and put it into the list
if(group != null)
{
groupNames.Add(group.Name);
}
}
这满足了您的需求吗?
有关System.DirectoryServices.AccountManagement
命名空间的更多信息,请阅读MSDN杂志中的Managing Directory Security Principals in the .NET Framework 3.5文章。
另一答案
这是我的解决方案;对于那些感兴趣的人:
public ArrayList getGroups()
{
// ACTIVE DIRECTORY AUTHENTICATION DATA
string ADDomain = "myDomain.local";
string ADBranchsOU = "Distribution Group";
string ADUser = "Admin";
string ADPassword = "password";
// CREATE ACTIVE DIRECTORY ENTRY
DirectoryEntry ADRoot
= new DirectoryEntry("LDAP://OU=" + ADBranchsOU
+ "," + getADDomainDCs(ADDomain),
ADUser,
ADPassword);
// CREATE ACTIVE DIRECTORY SEARCHER
DirectorySearcher searcher = new DirectorySearcher(ADRoot);
searcher.Filter = "(&(objectClass=group)(cn=* Region))";
SearchResultCollection searchResults = searcher.FindAll();
// ADDING ACTIVE DIRECTORY GROUPS TO LIST
ArrayList list = new ArrayList();
foreach (SearchResult result in searchResults)
{
string groupName = result.GetDirectoryEntry().Name.Trim().Substring(3);
list.Add(groupName);
}
return list;
}
public string getADDomainDCs(string ADDomain)
{
return (!String.IsNullOrEmpty(ADDomain))
? "DC=" + ADDomain.Replace(".", ",DC=")
: ADDomain;
}
以上是关于读取/过滤分发组的活动目录的子组?的主要内容,如果未能解决你的问题,请参考以下文章