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

标签 c# ldap openldap

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

相关文章:

c# - .net 框架的 AddMicrosoftIdentityWebApiAuthentication 模拟

c# - 用于十进制数字点而不是逗号的正则表达式 (.NET)

linux - LDAP 监听本地主机但主机名或 IP

c# - 我们可以使用基类对象访问派生类属性吗?

c# - 缓存对象的高效克隆

delphi - 如何在 Delphi 中对 Windows 域用户进行身份验证?

ssl - 如何从 OpenSSL 保存 LDAP SSL 证书

java - eDirectory 查询返回不愿意执行 LDAPSearchException

linux - Active Directory 上的 LDAP 搜索

c - 迭代 ldap 搜索结果集