Enumerating Active Directory Groups

I am requesting an enterprise LDAP list with the following code. The problem is that she writes out the full line. Is there an easy way to just write a group name other than parsing a string?

using System; using System.Collections.Generic; using System.DirectoryServices; using System.Linq; public class Test { public static void Main() { string userName = "USER"; DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://dc=ABC,dc=com"); DirectorySearcher search = new DirectorySearcher(); search.Filter = String.Format("(cn={0})", userName); search.PropertiesToLoad.Add("memberOf"); List<string> groupsList = new List<string>(); SearchResult result = search.FindOne(); if (result != null) { int groupCount = result.Properties["memberOf"].Count; for (int counter = 0; counter < groupCount; counter++) { groupsList.Add((string)result.Properties["memberOf"][counter]); } } List<string> list = new List<string>(); list = groupsList.ToList(); for (int i = 0; i < list.Count; i++) { Console.WriteLine(list[i]); } } } 
+4
source share
1 answer

I think the solution is simpler than that.

You are trying to find user groups, right?

 private void button1_Click(object sender, EventArgs e) { List<string> userGroups = new List<string>(); PrincipalContext LdapContext = new PrincipalContext(ContextType.Domain, domainName); UserPrincipal user = UserPrincipal.FindByIdentity(LdapContext, userName); foreach (var group in user.GetGroups()) { userGroups.Add(group.Name); } } 
+2
source

Source: https://habr.com/ru/post/1444093/


All Articles