我正在尝试使用对象类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);
}

...导致错误:

最佳答案

事实证明,您可以在条目首先存储到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/

10-11 21:04