public void GetGroupsUserIsMemberOf(string sAMAccountName) { try { using (DirectoryEntry dirEntry = new DirectoryEntry()) { dirEntry.Path = "LDAP://OU=OUName,DC=DCName,DC=org"; dirEntry.AuthenticationType = AuthenticationTypes.Secure; using (DirectorySearcher dirSearch = new DirectorySearcher(dirEntry)) { dirSearch.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(SAMAccountName={0}))", sAMAccountName); dirSearch.PropertiesToLoad.Add("memberOf"); SearchResult result = dirSearch.FindOne(); if (result != null) { int propCount = result.Properties["memberOf"].Count; for (int i = 0; i <= propCount - 1; i++) { // Clean up the name of the group for display purposes char[] delim = new char[] { ',', '\\' }; string groupName = result.Properties["memberOf"][i].ToString().Split(delim).First().Replace("CN=", string.Empty); Console.WriteLine(string.Format("Group# {0}: {1}", i+1, groupName); } } } } } }
Friday, September 14, 2012
Programmatically obtain a list of Active Directory groups that an individual is a member of
Here is some sample code that will allow you to obtain a complete list of all of the Active Director groups that a specified user is a member of:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.