Если вы создаете пользователя, вам необходимо
- привязать к контейнеру, в котором вы хотите создать пользователя, в
- создать новую учетную запись пользователя как дочернюю для этогоконтейнер
Просто задав путь 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", "pass@1w0rd01", 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) здесь: