eFeng.Leung

[爱我所爱][记我所想][学我所需]
posts - 25, comments - 13, trackbacks - 0, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

公告

Using Active Directory

Posted on 2006-04-04 16:23 eFeng.Leung 阅读(...) 评论(...) 编辑 收藏
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();
}