C#如何将条目添加到具有多个对象类的LDAP

问题描述 投票:4回答:2

我正在尝试使用对象类personuidObject在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请求的操作不满足一个或多个与对象类别相关的约束。 (来自的例外HRESULT:0x80072014)

c# ldap openldap
2个回答
6
投票

事实证明,您可以添加对象类之后该条目已首先存储到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();

0
投票

感谢您发贴,并了解如何比新搜索更有效地使用“ RefreshingCache”

很多技巧都可以使它正常工作也许用于“约束规则”的其他情况RGDS

© www.soinside.com 2019 - 2024. All rights reserved.