2012-03-21 121 views
2

我正在編寫一個將用戶添加到Active Directory的應用程序。我想使用此代碼連接到「用戶」共享文件夾中的AD使用LDAP向AD添加用戶

LDAP://celtestdomdc1.celtestdom.local/CN=Users,DC=celtestdom,DC=local 

但是它增加了在共享文件夾的用戶,而不是內部的「用戶」共享文件夾。不應該CN =用戶是否將它添加到「用戶」文件夾?

感謝

+0

我認爲marc_s在你的情況下是正確的。如果您發佈了一些代碼,我們可能會向您顯示所需的更改。 – user489041 2012-03-21 21:08:03

回答

3

如果你正在創建一個用戶,你需要

  • 綁定到容器要創建
  • 用戶創建新的用戶帳戶的一個孩子容器

僅僅通過設置LDAP路徑,您是而不是定義用戶將去的地方!

嘗試這樣的事情(C#示例 - 應該是微不足道轉換爲VB.NET):

DirectoryEntry cnUsers = new DirectoryEntry("LDAP://CN=Users,DC=celtestdom,DC=local"); 

// create a user directory entry in the container 
DirectoryEntry newUser = container.Children.Add("cn=NewUserAccount", "user"); 

// add the samAccountName mandatory attribute 
newUser.Properties["sAMAccountName"].Value = "NewUser"; 

// add any optional attributes 
newUser.Properties["givenName"].Value = "User"; 
newUser.Properties["sn"].Value = "One"; 

// save to the directory 
newUser.CommitChanges(); 

// set a password for the user account 
// using Invoke method and IadsUser.SetPassword 
newUser.Invoke("SetPassword", new object[] { "pAssw0rdO1" }); 

// require that the password must be changed on next logon 
newUser.Properties["pwdLastSet"].Value = 0; 

// save to the directory 
newUser.CommitChanges(); 

或者,如果你使用.NET 3.5或更新版本,你也可以使用新的命名空間System.DirectoryServices.AccountManagement這使得很多事情更容易。

然後代碼看起來有點簡單:

// create a context for a domain and define "base" container to use 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, 
     "celtestdom", "CN=Users,DC=celtestdom,DC=local"); 

// create a user principal object 
UserPrincipal user = new UserPrincipal(ctx, "NewUser", "[email protected]", true); 

// assign some properties to the user principal 
user.GivenName = "User"; 
user.Surname = "One"; 

// force the user to change password at next logon 
user.ExpirePasswordNow(); 

// save the user to the directory 
user.Save(); 

查看更多關於System.DirectoryServices.AccountManagement(S.DS.AM)命名空間位置: