Devo creare un nuovo utente in Active Directory. Ho trovato diversi esempi come i seguenti:Crea utente di Active Directory in .NET (C#)
using System;
using System.DirectoryServices;
namespace test {
class Program {
static void Main(string[] args) {
try {
string path = "LDAP://OU=x,DC=y,DC=com";
string username = "johndoe";
using (DirectoryEntry ou = new DirectoryEntry(path)) {
DirectoryEntry user = ou.Children.Add("CN=" + username, "user");
user.Properties["sAMAccountName"].Add(username);
ou.CommitChanges();
}
}
catch (Exception exc) {
Console.WriteLine(exc.Message);
}
}
}
}
Quando si esegue questo codice non ottengo errori, ma nessun nuovo utente viene creato.
L'account su cui sto eseguendo il test dispone di privilegi sufficienti per creare un utente nell'unità organizzativa di destinazione.
Mi manca qualcosa (forse qualche attributo obbligatorio dell'oggetto utente)?
Qualche idea sul perché il codice non fornisce eccezioni?
EDIT
Di seguito ha lavorato per me:
int NORMAL_ACCOUNT = 0x200;
int PWD_NOTREQD = 0x20;
DirectoryEntry user = ou.Children.Add("CN=" + username, "user");
user.Properties["sAMAccountName"].Value = username;
user.Properties["userAccountControl"].Value = NORMAL_ACCOUNT | PWD_NOTREQD;
user.CommitChanges();
quindi c'erano in realtà un paio di problemi:
CommitChanges
deve essere chiamatouser
(grazie Rob)- Il criterio password impediva all'utente di essere creato (grazie Marc)
Buona cattura !! :-) –
il potere di leggere "manuali" in azione :) – balexandre