C # How to add an entry in LDAP with multiple object classes

I am trying to create a new user entry in OpenLDAP using the person and uidObject object classes. The problem is that with System.DirectoryServices.DirectoryEntry I only found a way to add a new record with one class of objects, but not a way to add multiple classes of objects.

This c # code

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); } 

... results in an error:

-2147016684 The requested operation did not satisfy one or more of the restrictions associated with the object class. (Exception from HRESULT: 0x80072014)

+5
source share
1 answer

It turns out that you can add object classes after the record was first saved in LDAP and selected again. So, with a simple change, it works just fine!

 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(); 
+7
source

Source: https://habr.com/ru/post/1307761/


All Articles