Solutions: -
So you can select groups from OU to AD
DataTable dt = new DataTable();
dt.Columns.Add("groups");
DirectoryEntry rootDSE = null;
Suppose I want to receive records from my department unit. Now the path will be like
Department →> Members
dc - . Corp.Local
AD
if (department != "")
{
rootDSE = new DirectoryEntry(
"LDAP://OU=" + department + ",OU=Users,dc=corp,dc=local", username, password);
}
else
{
rootDSE = new DirectoryEntry(
"LDAP://OU=Users,OU=" + ou + ",dc=corp,dc=local", username, password);
}
DirectorySearcher ouSearch = new DirectorySearcher(rootDSE);
ouSearch.PageSize = 1001;
ouSearch.Filter = "(objectClass=group)";
ouSearch.SearchScope = SearchScope.Subtree;
ouSearch.PropertiesToLoad.Add("name");
SearchResultCollection allOUS = ouSearch.FindAll();
foreach (SearchResult oneResult in allOUS)
{
dt.Rows.Add(oneResult.Properties["name"][0].ToString());
}
rootDSE.Dispose();
return dt;
, .
, , Looping the Users.
PrincipalContext pr = new PrincipalContext(ContextType.Domain,
"corp.local", "dc=corp,dc=local", username, password);
GroupPrincipal group = GroupPrincipal.FindByIdentity(pr, groupName);
if (group == null)
{
}
UserPrincipal user = UserPrincipal.FindByIdentity(pr, userName);
if (user.IsMemberOf(group))
{
}
else
{
if (user != null & group != null)
{
group.Members.Add(user);
group.Save();
done = user.IsMemberOf(group);
}
}
pr.Dispose();
return done;