0

Active Directory の特定の OU からユーザーを取得する必要があるプロジェクトに取り組んでいます。

メニューを使用して、Dropdown存在するすべての OU を保存します。ユーザーが特定の OU を選択してボタンをクリックするとすぐに、その OU で利用可能なユーザーがテキストボックスに表示されます。

これは使用されているコードです:

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";
        }
    }

問題は button1_click 関数で発生しています。defaultcontext の場合、次のエラーがスローされます。

System.Runtime.InteropServices.COMException: The server is not operational.

このエラーの対処方法がわかりません。ある種のアセンブリがありませんか?

4

1 に答える 1