Julian
Julian

Reputation: 67

How to create a new AD user in a specific directory?

This is the code to create a new Active Directory user:

public string CreateUserAccount(string ldapPath, string userName, 
string userPassword)
{
    try
    {
        string oGUID = string.Empty;
        string connectionPrefix = "LDAP://" + ldapPath;
        DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
        DirectoryEntry newUser = dirEntry.Children.Add
            ("CN=" + userName, "user");
        newUser.Properties["samAccountName"].Value = userName;
        newUser.CommitChanges();
        oGUID = newUser.Guid.ToString();

        newUser.Invoke("SetPassword", new object[] { userPassword });
        newUser.CommitChanges();
        dirEntry.Close();
        newUser.Close();
    }
    catch (System.DirectoryServices.DirectoryServicesCOMException E)
    {
        //DoSomethingwith --> E.Message.ToString();

    }
    return oGUID;
}

But where is the user created? I have many subfolders in the AD and I would like to put the new user in a specific folder.

How can i commit a path while creating this new user?

Path example: domain/groupname/groupsubfolder/externalusers/user

Upvotes: 0

Views: 2434

Answers (1)

Am_I_Helpful
Am_I_Helpful

Reputation: 19158

But where is the user created?

string connectionPrefix = "LDAP://" + ldapPath;
        DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
        DirectoryEntry newUser = dirEntry.Children.Add
            ("CN=" + userName, "user");

Based on the code you've shared, the user will be created in the container addressed by the value ldapPath. Whatever value is passed in ldapPath, the user will be created inside that parent container.

I would like to put the new user in a specific folder. Path example: domain/groupname/groupsubfolder/externalusers/user

Since you're binding to the DirectoryEntry node with the help of a method argument, you need to pass the following value in place of ldapPath.

ldapPath = "OU=user,OU=externalusers,OU=groupsubfolder,OU=groupname,DC=domain,DC=name";
// assuming that user, externalusers, groupsubfolder and groupname are all OUs
// then place the call to method CreateUserAccount
// and pass this ldapPath as the string value.
CreateUserAccount(ldapPath, userName, userPassword);

How can i commit a path while creating this new user?

string connectionPrefix = "LDAP://" + ldapPath;
// if you change the ldapPath here to the OU where you want the user to be created,
// you'll get the desired result.
        DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
        DirectoryEntry newUser = dirEntry.Children.Add
            ("CN=" + userName, "user");

If you want to change the path of the user-creation, please change the value of ldapPath to the OU/Container where you want the user to be created as highlighted in the comment above.

The user is created as soon as you perform the first commit. Check this line in your code which is responsible for the user-creation:

    newUser.Properties["samAccountName"].Value = userName;
    newUser.CommitChanges();

Upvotes: 2

Related Questions