Click here to Skip to main content
15,887,946 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i am trying to create new user in AD i getting error in Access denied

C#
public int createUser( String domain, String first,
                             String last, String description, object[] password,
                             String username,String ema, bool enabled)
    {

          DirectoryEntry myLdapConnection = DirectEntry();
         
        DirectoryEntry user = dirEntry.Children.Add("CN=" + first + " " + last, "user");

        // User name (domain based)   
        user.Properties["userprincipalname"].Add(username + "@" + domain);

        // User name (older systems)  
        user.Properties["samaccountname"].Add(username);

        // Surname  
        user.Properties["sn"].Add(last);

        // Forename  
        user.Properties["givenname"].Add(first);

        // Display name  
        user.Properties["displayname"].Add(first + " " + last);

        // Description  
        user.Properties["description"].Add(description);

        // E-mail  
        user.Properties["mail"].Add(username+ "@" + ema);

       user.CommitChanges();
       user.Invoke("SetPassword", password);

        if (enabled)
            user.Invoke("Put", new object[] { "userAccountControl", "512" });

        user.CommitChanges();
        return 0;
    }
       static DirectoryEntry  DirectEntry( )  
      {  
          DirectoryEntry ldapConnection = new DirectoryEntry("rch");
         ldapConnection.Path = "LDAP://OU=CEO,OU=ORGANIZATION,DC=dac,DC=com"; 
         ldapConnection.AuthenticationType = AuthenticationTypes.Secure;  
         return ldapConnection;  
      }  
Posted
Comments
virusstorm 25-Jun-15 15:30pm    
The account your code is executing under does not have permission to add user.
$ultaNn 28-Jun-15 3:58am    
ok, i have a user with all permissions , how should i enter in AD with that user and create new user

1 solution

Got solution with username and password for directyentry.

C#
static DirectoryEntry  DirectEntry(string ou)  
      {  
        
         string ldapname = "sharepointtest";
         string name = "h.com.sa" + @"\" + ldapname;
          DirectoryEntry ldapConnection = new DirectoryEntry("rch");
          ldapConnection.Path = "LDAP://OU=CEO,OU=ORGANIZATION CHART,DC=h,DC=com";
          ldapConnection.Username = name;
          ldapConnection.Password = "123";
          return ldapConnection;  
      } 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900