Creating Users 

The following example shows how to create a user in an organizational unit. By default, this account will be disabled. To enable the account, set a password for it. For more information, see Managing User Passwords.

C#
DirectoryEntry ent = new DirectoryEntry();
DirectoryEntry ou = ent.Children.Find("OU=Consulting");

// Use the Add method to add a user to an organizational unit.
DirectoryEntry usr = ou.Children.Add("CN=New User","user");
// Set the samAccountName, then commit changes to the directory.
usr.Properties["samAccountName"].Value = "newuser"; 
usr.CommitChanges();

In this example, the samAccountName attribute is set. For more information about this attrobite, see the topic samAccountName in the MSDN Library at http://msdn.microsoft.com/library.

The samAccountName attribute creates a unique samAccountName, such as $CP2000-O16B1V0UKHK7. This attribute is required on the user account when the domain controller is running on Windows NT Server 4.0. In Windows Server 2003, the samAccountName attribute is optional.

Tags :


Page view tracker