using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
public static List<String> GetUserMemberOf(string domain, string username, string password)
{
DirectorySearcher searcher = null;
List<string> lMembers = new List<string>();
try
{
searcher = new DirectorySearcher(new DirectoryEntry(string.Concat(@"LDAP://", domain), username, password));
searcher.Filter = string.Concat(@"(&(objectClass=User) (sAMAccountName=", username, "))"); searcher.PropertiesToLoad.Add(
@"MemberOf");
SearchResult result = searcher.FindOne();
for (int i = 0; i < result.Properties[@"MemberOf"].Count; i++)
{
string sProp = result.Properties[@"MemberOf"][i].ToString(); lMembers.Add(sProp.Substring(3, sProp.IndexOf(@",") - 3));
}
}
catch (Exception)
{
throw;
}
finally
{
searcher.Dispose();
}
return lMembers;
}