从 Active Directory 组获取用户
我已经敲了好几个小时的头试图找出为什么这行不通。我找到了一个从 AD 组获取用户列表的示例,但我无法让它工作。这是我一直在尝试做的事情:
DirectoryEntry de = new DirectoryEntry("LDAP://DC=" + domain + ",DC=com");
DirectorySearcher ds = new DirectorySearcher(de);//, "(objectClass=person)");
ds.Filter = "(&(objectCategory=person)(objectclass=user)(memberOf=CN=!CityNameGroup))";
ds.PropertiesToLoad.Add("givenname");
ds.PropertiesToLoad.Add("samaccountname");
ds.PropertiesToLoad.Add("sn");
ds.PropertiesToLoad.Add("useraccountcontrol");
foreach (SearchResult sr in ds.FindAll())
{//stuff goes here}
但是 ds.FindAll
使用此过滤器总是返回 0 个结果。我可以做更简单的过滤器来返回结果,但我从来没有从上面的过滤器中得到任何结果。我只想要 !CityNameGroup 中的所有用户。我很感激你的帮助!
I have been banging my head for hours trying to figure out why this wont' work. I found an example of getting a list of users from an AD group but I can't get it to work. Here is what I've been trying to do:
DirectoryEntry de = new DirectoryEntry("LDAP://DC=" + domain + ",DC=com");
DirectorySearcher ds = new DirectorySearcher(de);//, "(objectClass=person)");
ds.Filter = "(&(objectCategory=person)(objectclass=user)(memberOf=CN=!CityNameGroup))";
ds.PropertiesToLoad.Add("givenname");
ds.PropertiesToLoad.Add("samaccountname");
ds.PropertiesToLoad.Add("sn");
ds.PropertiesToLoad.Add("useraccountcontrol");
foreach (SearchResult sr in ds.FindAll())
{//stuff goes here}
but ds.FindAll
always brings back 0 results with this filter. I can do simpler filters that bring back results, but I never get anything back from the above filter. I just want all my users that are in the !CityNameGroup. I appreciate the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果可以选择 .NET 3.5,请停止敲头并查看此处:
通过 C#.NET 3.5 访问 Active Directory 中的所有内容(使用System.DirectoryServices.AccountManagement)
说实话,.NET 3.5 中的 AD 处理是另一个世界。它将改变一切。当然是为了更好。
更新
此外,这里有一个现成的答案(Active Directory 用户组成员资格 GroupPrincipal )。我将有礼貌地不复制它。
:)
顺便说一句,它使用 .NET 3.5。
If .NET 3.5 is an option, stop banging your head and look here:
Everything in Active Directory via C#.NET 3.5 (Using System.DirectoryServices.AccountManagement)
Seriously, AD handling in .NET 3.5 is another world. It will change everything. For the better, of course.
Update
Also, there's a ready answer here ( Active Directory User Group Memberships GroupPrincipal ). I will have the decency of not copying it.
:)
It uses .NET 3.5, BTW.