我得到了一个可行的解决方案,但是我非常确定资源密集度较低的方法,因为当前的解决方案包括进行查询以获取组成员,然后查询以获取每个用户的信息 .
这是我的代码:
DirectoryEntry root = new DirectoryEntry( "LDAP://server:port" );
DirectorySearcher searcher = new DirectorySearcher( root );
searcher.Filter = "(&(ObjectClass=Group)(CN=foo-group))";
var members = (IEnumerable)searcher.FindOne()
.GetDirectoryEntry()
.Invoke( "members" );
Dictionary<string , string> results = new Dictionary<string , string>();
foreach( object member in members ) {
DirectoryEntry de = new DirectoryEntry( member );
results.Add( de.Properties[ "SAMAccountname" ][ 0 ].ToString(), de.Properties[ "cn" ][ 0 ].ToString() );
}
理想情况下,我希望能够执行单个查询以获取作为组成员的每个用户,过滤要加载的属性,然后显示它们 . 所以这样的事情
DirectoryEntry root = new DirectoryEntry( "LDAP://server:port" );
DirectorySearcher searcher = new DirectorySearcher( root );
searcher.PropertiesToLoad.Add( "cn" );
searcher.PropertiesToLoad.Add( "SAMAccountname" );
searcher.Filter = "(&(ObjectClass=user)(memberof=foo-group))";
foreach( var user in searcher.FindAll() ) {
//do whatever...
}
不幸的是,由于某些原因,这不起作用 .
谢谢你的帮助,
3 回答
如果你可以使用
System.DirectoryServices.AccountManagement
:我有一些VB代码也会以旧的方式执行,但这对于AccountManagement来说肯定更简单 .
这是我所指的VB代码(再次它不漂亮,但它的功能):
用法:
我最初使用的方法类似于您发布的方法,大约需要12分钟才能完成整个公司的广告并获得结果 . 切换到此方法后,大约需要2分钟 . 您需要使用我编写ldapserver的ldapserver地址以及用户ID和密码,用户名是您正在查找的人的SAMAccountName .
如果选中HERE,则可以执行以下操作: