我正在编写一个将用户添加到 Active Directory 的应用程序。我正在尝试使用此代码连接到 AD 中的“Users”共享文件夹
LDAP://celtestdomdc1.celtestdom.local/CN=Users,DC=celtestdom,DC=local
但是,它将用户添加到共享文件夹中,而不是添加到“用户”共享文件夹中。 CN=Users 不应该意味着它将把它添加到“Users”文件夹中吗?
谢谢
最佳答案
如果您要创建用户,则需要
- 绑定(bind)到您要在其中创建用户的容器
- 创建新用户帐户作为该容器的子级
仅通过设置 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) 命名空间的更多信息:
关于vb.net - 使用 LDAP 将用户添加到 AD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9812199/