vb.net - 使用 LDAP 将用户添加到 AD

标签 vb.net active-directory ldap

我正在编写一个将用户添加到 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/

相关文章:

vb.net - 类型未显示以供服务引用

asp.net - XPath 不返回节点值 ASP.NET 4.5

vb.net - 获取打印计数器 VB.net

active-directory - Active Directory 用户的主要标识符是 'logonname' 属性吗?

java - 使用 maven 时用于开发环境与部署的不同 spring XML 文件

c# - App.Config 可以嵌入到.NET 应用程序中吗?

java - Active Directory - 通过指定搜索过滤器和搜索控件进行搜索,无需上下文

ruby - 进入域后如何配置静默登录Redmine?

active-directory - AD 与 Liferay 同步

java - 使用自定义套接字工厂池化 LDAP 连接