SIGN UP MEMBER LOGIN:    
ARTICLE

Using Active Directory

Posted by Vivek Sharma Articles | Active Directory C# July 31, 2002
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.
Reader Level:

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

Login to add your contents and source code to this article
share this article :
post comment
 

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

Posted by hitender yadav Oct 22, 2007

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?

Posted by G C Jul 18, 2007
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor