Using Active Directory


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

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now