Vb.net 使用LDAP向AD添加用户

Vb.net 使用LDAP向AD添加用户,vb.net,active-directory,ldap,Vb.net,Active Directory,Ldap,我正在编写一个将用户添加到Active Directory的应用程序。我正在尝试使用此代码连接到广告中的“用户”共享文件夹 LDAP://celtestdomdc1.celtestdom.local/CN=Users,DC=celtestdom,DC=local 但是,它会将用户添加到共享文件夹中,而不是“用户”共享文件夹中。CN=Users难道不意味着它将把它添加到“Users”文件夹中吗 谢谢如果要创建用户,您需要 绑定到要在其中创建用户的容器 将新用户帐户创建为该容器的子级 只需设置

我正在编写一个将用户添加到Active Directory的应用程序。我正在尝试使用此代码连接到广告中的“用户”共享文件夹

LDAP://celtestdomdc1.celtestdom.local/CN=Users,DC=celtestdom,DC=local
但是,它会将用户添加到共享文件夹中,而不是“用户”共享文件夹中。CN=Users难道不意味着它将把它添加到“Users”文件夹中吗


谢谢

如果要创建用户,您需要

  • 绑定到要在其中创建用户的容器
  • 将新用户帐户创建为该容器的子级
只需设置LDAP路径,就可以而不是定义用户的去向

尝试类似的方法(C#示例-转换为VB.NET应该很简单):

或者,如果您使用的是.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)命名空间的更多信息:


我认为marc_s在你的情况下是正确的。如果您发布一些代码,我们可能会向您展示所需的更改。
// 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();