【问题标题】:Not able to fetch users from the OU无法从 OU 获取用户
【发布时间】:2014-07-08 10:50:17
【问题描述】:

我正在从事一个项目,我必须从 Active Directory 中的特定 OU 获取用户。

我使用Dropdown 菜单来存储所有存在的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 函数中。对于默认上下文,它会引发以下错误:

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

我无法弄清楚如何解决此错误。我是否缺少某种程序集?

【问题讨论】:

    标签: c# active-directory ou


    【解决方案1】:

    改变这一行

    ouName = deResult.Properties["ou"][0].ToString();
    

    ouName = deResult.Properties["distinguishedName"][0].ToString();
    

    我想你会没事的

    【讨论】:

    • 点击按钮时出错。该代码工作正常。
    • 您在按钮点击代码中传递的路径来自您在下拉列表中填充的内容。您在下拉列表中输入的值对绑定无效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    相关资源
    最近更新 更多