• How to create user in active directory using LDAP

    Posted on June 29, 2012 by in Dotnet Tips & Tricks, Tips & Tricks

    public static void CreateUser(string username, string password)
    {
        //set active directory search base
        //DOMAIN PATH PATTERN: CN=Name,CN=Directory,DC=Domain,DC=com or simply server name
        DirectoryEntry directoryEntry = new DirectoryEntry(CONST_DOMAIN_PATH, CONST_AD_USERNAME, CONST_AD_PASSWORD);
    
        //Creates a new entry in the container.
        DirectoryEntry newUser = directoryEntry.Children.Add("CN=" + username, "user");
        
        //set some properties such as account name, given name etc
        newUser.Properties["sAMAccountName"].Add(username);
    
        //Saves changes that are made to a directory entry to the AD
        newUser.CommitChanges();
    
        //set user password
        newUser.Invoke("SetPassword", new object[] { password });
        Console.WriteLine(newUser.Guid.ToString());
    
        //Close the System.DirectoryServices.DirectoryEntry object and releases any
        //system resources that are associated with this component.
        directoryEntry.Close();
        newUser.Close();
    }
    Be Sociable, Share!
      Post Tagged with

    Written by

    View all articles by

    Email : [email protected]

    Leave a Reply