0

I'm trying to add users to Active Directory and my code so far is

using (DirectoryEntry dirEntry = new DirectoryEntry(ldapPath))
    if (dirEntry.SchemaEntry.Name == "container")
    {
        using (DirectoryEntry newUser = dirEntry.Children.Add("CN= " + username, "User"))
        {
            fullname = fname + " " + lname;
            newUser.Properties["sAMAccountName"].Value = username;
            newUser.Properties["First name"].Value = fname;
            newUser.Properties["Last name"].Value = lname;
            newUser.Properties["Full name"].Value = fullname;
            newUser.Properties["password"].Value = password;
            newUser.CommitChanges();
        }
    }

When I run the program I get the error

The specified directory service attribute or value does not exist.

Any suggestions on how I can make this work? And yes I'm new to Active Directory related stuff.

4
  • can you edit this to be readable? Commented Jun 21, 2015 at 20:04
  • at which line are you getting the error? Commented Jun 21, 2015 at 20:04
  • i'm getting the error at " newUser.CommitChanges();" Commented Jun 21, 2015 at 20:05
  • When I manually make the account on the active directory, I only need to enter first name, last name, (full name is done by itself), username, and password Commented Jun 21, 2015 at 20:08

1 Answer 1

2

The Active Directory attributes need to be addressed by their LDAP names - not what you see in the GUI....

So try this:

using (DirectoryEntry dirEntry = new DirectoryEntry(ldapPath))
{
    if (dirEntry.SchemaEntry.Name == "container")
    {
        using (DirectoryEntry newUser = dirEntry.Children.Add("CN=" + username, "User"))
        {
             fullname = fname + " " + lname;
             newUser.Properties["sAMAccountName"].Value = username;
             newUser.Properties["givenName"].Value = fname;  // first name
             newUser.Properties["sn"].Value = lname;    // surname = last name
             newUser.Properties["displayName"].Value = fullname;  
             newUser.Properties["password"].Value = password;

             newUser.CommitChanges();
         }
    }
}

You can find a great Excel spreadsheet showing the names used in the interactive GUI, and what LDAP names they map to, on Richard Mueller's web site here (check out the "Spreadsheet of all Active Directory attributes" and "Spreadsheet of User Properties in Active Directory Users & Computers MMC.")

Or if you're using .NET 3.5 or newer, you could also investigate the new System.DirectoryServices.AccountManagement namespace, which allows you to use nicely shaped objects to handle common tasks.

Your code would look something like this:

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, NULL, ldapPath)
{
    // create a user principal object
    UserPrincipal user = new UserPrincipal(ctx, username, password, true);

    // assign some properties to the user principal
    user.GivenName = fname;
    user.Surname = lname;
    user.DisplayName = fullname;

    // save the user to the directory
    user.Save();
}

Note: the ldapPath should be the container's LDAP path - without any prefixes, e.g. something like CN=Users,DC=YourCompany,DC=com - no LDAP:// or other prefixes.

The plus side is: the UserPrincipal object class already contains nice, strongly-typed and more intuitive properties to handle many of the basic tasks, like creating a new user and setting some of its properties.

Sign up to request clarification or add additional context in comments.

8 Comments

I tried your first Solution and it gave me the same error, I'm trying your second solution but "UserPrincipal user= ..." is underlined and it says "invalid initializer member declarator"
On the second solution i'm getting the error "Unknown error (0x80005000)"
@GK28: what do your ldapPath and username look like? Is the username valid and unique within its container?
string ldapPath = "LDAP://CN=Users,DC=YasirTek,DC=com"; and for the username it's just "JJill" since it's test account i'm trying to create
UserPrincipal user = new UserPrincipal(ctx, username, password, true); For username and password on this, I ofcourse used my credentials which are full admit rights. but end up getting the unknown error (0x80005000)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.