2014-07-08 4 views
0

Active Directory의 특정 OU에서 사용자를 가져와야하는 프로젝트에서 작업하고 있습니다.OU에서 사용자를 가져올 수 없습니다.

나는 모든 OU를 저장하기 위해 Dropdown 메뉴를 사용합니다. 사용자가 특정 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.

나는이 오류에 대해 이동하는 방법을 알아낼 수 아니에요 :의 DefaultContext를 들어 다음과 같은 오류가 발생합니다. 어떤 종류의 어셈블리가 빠졌습니까?

답변

0

변경

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

이 라인

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

그리고 당신이

+0

내 오류가 버튼 클릭에서 발생한 괜찮을 것 같아요. 그 코드는 잘 작동합니다. – Esha

+0

버튼 클릭 코드에서 전달하는 경로는 드롭 다운 목록에있는 경로에서옵니다. 드롭 다운 목록에 넣은 값은 바인딩에 유효하지 않습니다. –