Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello im trying to create users in the AD with my code, i have a standard code that creates a user, but i would like to add some things such as SamAccountName + firstname + Lastname, all the code does now is to create a user called "TESTUSER" without any samaccount/passowrod etc

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {


            string ADFullPath = "LDAP://192.168.0.10/OU=T2,OU=root,DC=admin,DC=domain,DC=com";
            DirectoryEntry ouEntry = new DirectoryEntry(ADFullPath, "admin", "passw0rd", AuthenticationTypes.Secure); 


            for (int i = 0; i < 1; i++)
            {
                try
                {
                    DirectoryEntry childEntry = ouEntry.Children.Add("CN=TESTUSER" + i, "user");
                    childEntry.CommitChanges();
                    ouEntry.CommitChanges();
                    childEntry.Invoke("SetPassword", new object[] { "password22" });
                    childEntry.CommitChanges();
                    
                }
                catch (Exception ex)
                {


                }


            }


        }
    }
}
Posted

Have you bothered to do any substantial research? There are hundreds of articles on how to do this in various forms.
There are even a few on this site.

Infact this one:

Howto: (Almost) Everything In Active Directory via C#[^]

Comes top of a Google search for "c# active directy user creation" (Yes I even spelt directory wrong and it found it).

It should tell you everything you need to know.
 
Share this answer
 
I have actually read just that article, but as many who tries it, some things there doesn't work. For example the section called "create_user.cs:"
It's supposed to create a AD user but instead it generates alot of errors in the console.

So i found this code that i just posted now, which acctually work's but i need to add some features to it that's why i asked =)
 
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