2010-04-23 20 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(); 
相關問題