C # Come aggiungere una voce a LDAP con più classi di oggetti
Domanda
Sto cercando di creare un nuovo record utente in OpenLDAP con classi di oggetti persona e uidObject . Il problema sembra essere che con System.DirectoryServices.DirectoryEntry ho trovato solo un modo per aggiungere una nuova voce con una classe di oggetti, ma non è un modo per aggiungere più classi di oggetti.
Il codice C #
DirectoryEntry nRoot = new DirectoryEntry(path);
nRoot.AuthenticationType = AuthenticationTypes.None;
nRoot.Username = username;
nRoot.Password = pwd;
try
{
DirectoryEntry newUser = nRoot.Children.Add("CN=" + "test", "person");
newUser.Properties["cn"].Add("test");
newUser.Properties["sn"].Add("test");
newUser.Properties["objectClass"].Add("uidObject"); // this doesnt't make a difference
newUser.Properties["uid"].Add("testlogin"); // this causes trouble
newUser.CommitChanges();
}
catch (COMException ex)
{
Console.WriteLine(ex.ErrorCode + "\t" + ex.Message);
}
... risultati in errore:
-2.147,016684 millions L'operazione richiesta non soddisfaceva uno o più vincoli associati alla classe dell'oggetto. (Eccezione da HRESULT: 0x80072014)
Soluzione
Si scopre che è possibile aggiungere classi di oggetti dopo la voce è stato memorizzato prima a LDAP e prese di nuovo. Così, con un semplice cambiamento funziona bene!
DirectoryEntry newUser = nRoot.Children.Add("CN=" + "test", "person");
newUser.Properties["cn"].Add("test");
newUser.Properties["sn"].Add("test");
newUser.CommitChanges();
newUser.RefreshCache();
newUser.Properties["objectClass"].Add("uidObject");
newUser.Properties["uid"].Add("testlogin");
newUser.CommitChanges();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow