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

百般思念 提交于 2019-12-06 08:11:20

问题


Using the .net 3.5 framework and C# I'm trying to add a new user to AD from C# and can't find any examples. I see that the PrincipalCollection object has an overloaded 'add' method but can't seem to figure out how it works. Can anyone help?

How create a new user object, add it into AD.

Secondly, the user that will be adding in new people may not actually have the security to do this. Is there a way that I can impersonate another user account that will have permissions and add the account that way?


回答1:


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.



来源:https://stackoverflow.com/questions/912040/how-do-i-add-a-user-to-ad-using-system-directoryservices-accountmanagement

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