Adding an Active Directory User [C#/.NET]

http://www.thejackol.com/2004/07/30/adding-an-active-directory-user-cnet/

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;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s