如何使用System.DirectoryServices.AccountManagement将用户添加到AD?

使用.net 3.5框架和C#我试图从C#向AD添加新用户,但找不到任何示例。 我看到PrincipalCollection对象有一个重载的’add’方法,但似乎无法弄清楚它是如何工作的。 有人可以帮忙吗?

如何创建新的用户对象,将其添加到AD中。

其次,将要添加新用户的用户可能实际上没有安全性来执行此操作。 有没有办法可以冒充其他具有权限的用户帐户并以这种方式添加帐户?

您可以添加如下用户:

using (var context = new PrincipalContext(ContextType.Domain)) using (var user = new UserPrincipal(context) { UserPrincipalName = "username", Enabled = true }) { user.SetPassword("password"); user.Save(); } 

Re:安全性您可以将应用程序池标识设置为使用具有写入Active Directory权限的特权服务帐户。 或者,您可以为PrincipalContext使用构造函数重载,该重载使用LDAP连接的用户名和密码。