DirectoryEntry deDirEntry = new DirectoryEntry("LDAP://server.com");
DirectorySearcher mySearcher = new DirectorySearcher(deDirEntry);
mySearcher.PropertiesToLoad.Add("distinguishedName");
string sFilter = String.Format("(&(objectcategory=group)(cn=" + groupName + "))");
mySearcher.Filter = sFilter;
mySearcher.Sort.Direction = SortDirection.Ascending;
mySearcher.Sort.PropertyName = "cn";
SearchResult result;
DirectoryEntry ResultEntry;
result = mySearcher.FindOne();
if (result == null)
{
return;
}
ResultEntry = result.GetDirectoryEntry();
groupName = ResultEntry.Properties["distinguishedName"].Value.ToString();
mySearcher = new DirectorySearcher(deDirEntry);
mySearcher.PropertiesToLoad.Add("samaccountname");
mySearcher.PropertiesToLoad.Add("cn");
mySearcher.PropertiesToLoad.Add("mail");
sFilter = String.Format("(&(memberOf={0}))", groupName);
mySearcher.Filter = sFilter;
mySearcher.Sort.Direction = SortDirection.Ascending;
mySearcher.Sort.PropertyName = "samaccountname";
SearchResultCollection results;
results = mySearcher.FindAll();
if (results == null)
{
return;
}
foreach (SearchResult resEnt in results)
{
ResultPropertyCollection propcoll = resEnt.Properties;
User user = new User();
foreach (string key in propcoll.PropertyNames)
{
if (key == "samaccountname")
{
foreach (object values in propcoll[key])
{
user.Username = values.ToString();
}
}
else if (key == "cn")
{
foreach (object values in propcoll[key])
{
user.Name = values.ToString();
}
}
else if (key == "mail")
{
foreach (object values in propcoll[key])
{
user.Email = values.ToString();
}
}
}
if (user.Username.Contains(" ") || user.Username.Contains("-"))
{
GetMembersFromGroup(user.Username);
}
else
{
if (!UserAlreadyExists(user))
list.Add(user);
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment