1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
const DOMAIN_AND_PORT = "my.domain.com:389";
const OU = "OU=myGroups,DC=my,DC=domain,DC=com";
// Definit la racine de ton AD
string root = "LDAP://"+DOMAIN_AND_PORT+"/"+OU;
// Instancie la directoryEntry racine
rootEntry = new DirectoryEntry(root);
// Definit un contexte de recherche avec la rootEntry comme baseDn
DirectorySearcher rootSearcher = new DirectorySearcher(rootEntry);
// Definit le filtre LDAP qui permet de retrouver le groupe Admin qui se trouve dans [OU]
rootSearcher.Filter = "(objectSID = \01 \02 \00 \00 \00 \00 \00 \05 \20 \00 \00 \00 \20 \02 \00 \00)";
// Lance la recherche
SearchResult sr = rootSearcher.FindOne();
if(sr != null){
// le groupe a été trouvé, on l'instancie
DirectoryEntry e = new DirectoryEntry();
e.Path = sr.Path;
// on met à jour l'attribut member du groupe en rajoutant le DN du user
e.Properties["member"].Add(NewUser.Path.ToString());
// Surtout ne pas oublier le commit des modifications
e.CommitChanges();
} |
Partager