我正在尝试使用对象类 person 和 uidObject 在 OpenLDAP 中创建一个新的用户记录。问题似乎是,对于 System.DirectoryServices.DirectoryEntry,我只找到了一种添加具有一个对象类的新条目的方法,但没有找到添加多个对象类的方法。
这个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);
}
...导致错误:
-2147016684 The requested operation did not satisfy one or more constraints associated with the class of the object. (Exception from HRESULT: 0x80072014)
最佳答案
事实证明,您可以在条目首次存储到 LDAP 并再次获取之后 添加对象类。因此,只需进行简单的更改,它就可以正常工作!
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();
关于C# 如何向具有多个对象类的 LDAP 添加条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2697232/