c# - C# - 使用 LDAP 检索 Active Directory 的组成员
问题描述
我的 Active Directory 中有一个“我的组”组,我想使用 Active Directory LDAP 从该组中检索用户。
如何修改我的查询以包含组并从中获取成员?
string username = “ldapuser”;
string password = “prime812”;
DirectoryEntry de = new DirectoryEntry(“LDAP://AM-LDAP-SN.ams.com/389/CN=Users,DC=ms,DC=ds,DC=AMS,dc=com”, username,password);
DirectorySearcher deSearch = new DirectorySearcher(de);
deSearch.SearchScope = SearchScope.Subtree;
forreach(SearchResult sResultSet in deSearch.FindAll())
{
}
解决方案
我会使用 PrincipalContext 而不是:
private static List<Principal> GetUsersOfGroup(string groupname)
{
string username = "ldapuser";
string password = "prime812";
using(var pc = new PrincipalContext(ContentType.Domain, null, username, password))
{
var _users = new List<Principal>();
var _group = GroupPrincipal.FindByIdentity(pc, groupname);
foreach(var member in group.GetMembers())
{
if(member is UserPrincipal _user)
_users.Add(_user);
}
return _users;
}
}
推荐阅读
- ip - 在笔记本电脑和 PC 上运行 nmap 时获取不同的 MAC 地址
- python - 将每日股票价格变成每周/每月/每季度/学期/每年?
- mysql - 当我在 where 子句中每行只有一个唯一变量时,如何返回多行?
- r - R,从向量中查找字符串,创建新的 TRUE/FALSE 列
- python - 如何在 CPU 上的 pyopencl 和 numpy 之间桥接数组?
- django - Django复选框不返回任何内容
- flutter - 带有 StickyHeader 的 Sliver 内的 TabBarView
- spring - Spring Boot 应用程序中每个端点 uri 的 HTTP 指标
- python-3.x - tf.keras.Model.fit 方法的 TensorFlow 问题
- java - 对象数组中的空指针异常