Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following code is creating user in Root of Active Directory.
But Now I need to change the code to create User in specific OU (Ou Nmae: OUMeetingUser)
I have tried this (by changing Line No.7)
C#
dirEntry.Path = "LDAP://ou=OUMeetingUser, dc=" + DomainSubhead.DomainNameUser;

But it is showing an unknown error.


C#
private void CreateNewuserAccount(string userName, string password)
        {
            string oGUID = string.Empty;
            try
            {
                DirectoryEntry dirEntry = new DirectoryEntry();
                dirEntry.Path = "LDAP://" + DomainSubhead.DomainNameUser;
                dirEntry.Username = DomainSubhead.userNameUser;
                dirEntry.Password = DomainSubhead.PasswordUser;
                //dirEntry.Parent.

                DirectoryEntry newUser = dirEntry.Children.Add("CN=" + userName, "user");

               //public static string ldapPath = "OU=Domain Users,DC=contoso,DC=com";

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

                newUser.Invoke("SetPassword", new object[] { password });


                //-----------------------
                newUser.Properties["userprincipalname"].Add(userName);

                // User name (older systems)  
                newUser.Properties["samaccountname"].Add(userName);

                // Surname  
                newUser.Properties["sn"].Add(userName);

                // Forename  
                newUser.Properties["givenname"].Add(userName);

                // Display name  
                newUser.Properties["displayname"].Add(userName);

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

                // E-mail  
                newUser.Properties["mail"].Add(txtNewUserEmail.Text);


                int val = (int)newUser.Properties["userAccountControl"].Value;
                newUser.Properties["userAccountControl"].Value = val & ~0x2;


                newUser.CommitChanges();
                dirEntry.Close();
                newUser.Close();

                
            }
            catch
            {
            }
            
        }


Can any one help me.
Posted

1 solution

C#
//ADPath1 = LDAP://172.16.0.1/OU=OUMeetingUser,DC=mydomain,DC=local
//ADUser = AD Admin User
//ADPassword = AD Admin user's password
 DirectoryEntry de = new DirectoryEntry(ADPath1, ADUser, ADPassword, AuthenticationTypes.Secure);
            // 1. Create user account
            DirectoryEntries users = de.Children;
            DirectoryEntry newuser = users.Add("CN=" + userid, "user");
           //2. Set properties
//....

newuser.CommitChanges();
 
Share this answer
 
Comments
Member 15657880 1-Jun-22 13:24pm    
I have used to this code but I have facing issue

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