I'm working on a project where I have to fetch the users from a particular OU in Active Directory.
I use the Dropdown
menu to store all the OUs present. As soon as the user selects a particular OU and clicks on the button the users available for that should be displayed in the textbox.
This is the code being used:
public MainWindow()
{
InitializeComponent();
DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
string defaultContext = rootDSE.Properties["defaultNamingContext"][0].ToString();
DirectoryEntry domainRoot = new DirectoryEntry("LDAP://" + defaultContext);
DirectorySearcher ouSearcher = new DirectorySearcher(domainRoot);
ouSearcher.SearchScope = SearchScope.Subtree;
ouSearcher.PropertiesToLoad.Add("ou");
ouSearcher.PropertiesToLoad.Add("cn");
ouSearcher.SearchScope = SearchScope.Subtree;
ouSearcher.Filter = "(objectCategory=organizationalUnit)";
try
{
comboBox1.SelectedIndex = 0;
comboBox1.Items.Insert(0, "Select An OU");
string ouName;
foreach (SearchResult deResult in ouSearcher.FindAll())
{
ArrayList alObjects = new ArrayList();
ouName = deResult.Properties["ou"][0].ToString();
comboBox1.Items.Insert(1, ouName.ToString());
}
}
catch (Exception ex2)
{
}
}
private void button1_Click(object sender, RoutedEventArgs e) //Error is present in this part
{
string selectou = comboBox1.SelectedValue.ToString();
DirectoryEntry rootDSE = new DirectoryEntry("LDAP://" + selectou);
string defaultContext = rootDSE.Properties["defaultNamingContext"][0].ToString(); //Here is the problem
DirectoryEntry domainRoot = new DirectoryEntry("LDAP://" + selectou);
DirectorySearcher ouSearcher = new DirectorySearcher(selectou);
ouSearcher.SearchScope = SearchScope.Subtree;
ouSearcher.PropertiesToLoad.Add("cn");
ouSearcher.SearchScope = SearchScope.Subtree;
ouSearcher.Filter = "(&(objectClass=user)(objectCategory=person))";
foreach (SearchResult deResult in ouSearcher.FindAll())
{
ArrayList alObjects = new ArrayList();
string dcName = deResult.Properties["cn"][0].ToString();
textBox1.Text = textBox1.Text + dcName.ToString() + "\n";
}
}
Now the problem is occuring at button1_click function. For defaultcontext it throws the following error:
System.Runtime.InteropServices.COMException: The server is not operational.
I'm not able to figure out how to go about this error. Am I missing some kind of assemblies?