[英]C# - Retrieve Group members of Active Directory using LDAP
I have a group "My Group" in my Active Directory, i would like to retrieve the users from this group using Active Directory LDAP.我的 Active Directory 中有一个“我的组”组,我想使用 Active Directory LDAP 从该组中检索用户。
How do i modify my query to include Group and get the members from it?如何修改我的查询以包含组并从中获取成员?
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())
{
}
I would use PrincipalContext isntead:我会使用 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.