This sample code can sense current context in Active Directory, checks for the existence of attributes, provides a list of all the attributes in Active Directory and can add a user to Active Directory.
public void AddUser()
{
string strDomain="";
String RootDSE;
System.DirectoryServices.DirectorySearcher DSESearcher = new System.DirectoryServices.DirectorySearcher();
RootDSE=DSESearcher.SearchRoot.Path;
MessageBox.Show(RootDSE);
RootDSE=RootDSE.Insert(7,"CN=Users,");
MessageBox.Show(RootDSE);
MessageBox.Show(strDomain);
DirectoryEntry myDE = new DirectoryEntry(RootDSE);
DirectoryEntries myEntries = myDE.Children;
// Create a new entry 'Sample' in the container.
DirectoryEntry myDirectoryEntry = myEntries.Add("CN="+txtUserName.Text, "user");
MessageBox.Show(myDirectoryEntry.SchemaClassName.ToString());
myDirectoryEntry.Properties["userPrincipalName"].Value=txtPrincipal.Text;
myDirectoryEntry.Properties["name"].Value=txtUserName.Text;
myDirectoryEntry.Properties["samAccountName"].Value=txtUserName.Text;
myDirectoryEntry.CommitChanges();
}
private void button2_Click(object sender, System.EventArgs e)
{
String RootDSE;
System.DirectoryServices.DirectorySearcher DSESearcher = new System.DirectoryServices.DirectorySearcher();
RootDSE=DSESearcher.SearchRoot.Path;
MessageBox.Show("Searching for user Class in "+RootDSE);
DirectoryEntry entry = new DirectoryEntry(RootDSE);
System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
mySearcher.Filter = ("(ObjectClass=user)");
System.DirectoryServices.SearchResult resEnt=mySearcher.FindOne();
entry=resEnt.GetDirectoryEntry();
MessageBox.Show("Entry Of attribute found = " + entry.Properties.Contains(txtAtt.Text).ToString());
}
private void button3_Click(object sender, System.EventArgs e)
{
String RootDSE;
System.DirectoryServices.DirectorySearcher DSESearcher = new System.DirectoryServices.DirectorySearcher();
RootDSE=DSESearcher.SearchRoot.Path;
MessageBox.Show("Searching for user Class in "+RootDSE);
DirectoryEntry entry = new DirectoryEntry(RootDSE);
System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
mySearcher.Filter = ("(ObjectClass=user)");
System.DirectoryServices.SearchResult resEnt=mySearcher.FindOne();
entry=resEnt.GetDirectoryEntry();
System.Collections.IEnumerator en = entry.Properties.PropertyNames.GetEnumerator();
while(en.MoveNext())
{
listBox1.Items.Add(en.Current.ToString());
}
}
public void AddUser1()
{
string strDomain="";
string RootDSE;
System.DirectoryServices.DirectorySearcher DSESearcher = new System.DirectoryServices.DirectorySearcher();
RootDSE=DSESearcher.SearchRoot.Path;
MessageBox.Show(RootDSE);
MessageBox.Show(RootDSE);
MessageBox.Show(strDomain);
DirectoryEntry myDE = new DirectoryEntry(RootDSE);
DirectoryEntries myEntries = myDE.Children;
// Create a new entry 'Sample' in the container.
DirectoryEntry myDirectoryEntry = myEntries.Add("CN="+txtUserName.Text, "CB");
MessageBox.Show(myDirectoryEntry.SchemaClassName.ToString());
MessageBox.Show(myDirectoryEntry.Path.ToString());
System.Collections.IEnumerator en = myDirectoryEntry.Properties.PropertyNames.GetEnumerator();
while(en.MoveNext())
{
MessageBox.Show(en.Current.ToString());
}
myDirectoryEntry.CommitChanges();
}
hitender yadavPosted Oct 22, 2007, 6:13 AM
i am trying to add user accounts in active directory but i am not able to write a code for the same, i have following data that needs to be added in the user account properties. First Name, Last Name, Lan ID, Employee ID (Should be entered in the description field in AD), Change Password (Yes check Mark which is always enabled), Home Drive (should display the path of the drive + lan id), Department (considering 3 departments). now i am not able to understand where to put or write the code (in seperate file or the same details.aspx file) and when to call which function to get the user accounts created. please help me writing this code so that i can learn some advanced things in asp.net. Thanks in advance
G CPosted Jul 18, 2007, 12:50 PM
Hi I create a user account in active directory and set the profile value (fill the "homedirectory" and "homedrive" properties and update user account). When I do this steps in "Active Directory Users and Computers", a folder was created and user access the folder. When I do this with program (mentioned above), properties value is set in active directory but the user folder does not create.Can you help me?