簡體   English   中英

無法從OU獲取用戶

[英]Not able to fetch users from the OU

我正在一個項目中,我必須從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函數中出現了問題。 對於defaultcontext,它將引發以下錯誤:

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

我不知道如何解決此錯誤。 我是否缺少某種裝配?

更改此行

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

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

我想你會沒事的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM