2010-04-23 42 views
4

我想创建一个新的用户记录到OpenLDAP与对象类uidObject。这个问题似乎是与System.DirectoryServices.DirectoryEntry我发现只有一种方法来添加一个对象类,但没有添加多个对象类的方式。C#如何添加一个条目到LDAP与多个对象类

此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)

回答

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(); 
相关问题