How do I add a user to AD using System.DirectoryServices.AccountManagement?

≡放荡痞女 提交于 2019-12-04 14:50:57

You can add a user like this:

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

Re: security you can set the application pool identity to use a privileged service account that has permission to write to the Active Directory. Or you can use a constructor overload for PrincipalContext that takes a username and password for the LDAP connection.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!