Adding an Active Directory User [C#/.NET]
So how do we add an Active Directory User?
public string CreateADSUser(string username, string password, string homedir) { String RootDSE;try { if (!Directory.Exists(homedir)) Directory.CreateDirectory(homedir);DirectorySearcher DSESearcher = new DirectorySearcher(); RootDSE = DSESearcher.SearchRoot.Path;RootDSE = RootDSE.Insert(7, "CN=Users,");DirectoryEntry myDE = new DirectoryEntry(RootDSE); DirectoryEntries myEntries = myDE.Children;DirectoryEntry myDirectoryEntry = myEntries.Add("CN=" + username, "user"); myDirectoryEntry.Properties["userPrincipalName"].Value = username; myDirectoryEntry.Properties["name"].Value = username; myDirectoryEntry.Properties["Password"].Value = password; myDirectoryEntry.Properties["samAccountName"].Value = username; myDirectoryEntry.Properties["FullName"].Value = username; myDirectoryEntry.Properties["AccountDisabled"].Value = 0; myDirectoryEntry.Properties["PasswordRequired"].Value = 1; myDirectoryEntry.Properties["HomeDirectory"].Value = homedir;// Permanent Password? myDirectoryEntry.Properties["permpass"].Value = 1; myDirectoryEntry.CommitChanges();DSESearcher.Dispose(); myDirectoryEntry.Dispose();return "Worked!"; } catch (Exception ex) { return ex.Message; } }
[update] Just found a class by Craig Aroa called ADHelper . It lets you do all this and more.


It’s work TNX !
it’s work-ING
Cool, or you could use the A.D. Advantage c# library (ad-advantage.net) to do all that with a single line of code.
Thanks For Your Efforts ?