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

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

How to create a new custom object, add it to AD.

Secondly, a user who will add new people may not have security for this. Is there a way that I can impersonate another user account that will have permissions and add the account this way?

+3
source share
1 answer

You can add a user as follows:

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 identifier to use a privileged service account that has write permissions in Active Directory. Or you can use the constructor overload for PrincipalContext, which takes the username and password for the LDAP connection.

+1
source

Source: https://habr.com/ru/post/1709115/


All Articles