2013-10-09 205 views
2

在我的開發機器上,我有一系列的虛擬機。其中一個是域控制器。域控制器的確在工作,因爲我無法登錄到其他虛擬機,而無需對其進行身份驗證。爲什麼我的LDAP查詢失敗?

我想測試的此DC LDAP查詢,並不停地進行故障

MY域控制器樹是這個樣子:

  • DC機名稱= ESDEV-DC01
  • Active Directory名稱= ESDEV.COM
  • 目標節點的規範名稱= ESDEV.COM/Users

MY SUBTREE具體目標如下所示:

  • 屬性名稱= objectCategory
  • 屬性值= CN =人,CN =架構,CN =配置,DC = ESDEV,DC = COM

我的參數是:

  • d irectoryPath = "LDAP://OU=Users, DC=ESDEV-DC01,DC=ESDEV,DC=Com"
  • SearchFilter = "(&(objectCategory=Person))"

問題:

我不斷收到 「出現在服務器上沒有這樣的對象」。

  1. 這是否意味着它正在查找服務器目錄?
  2. 爲什麼查詢失敗?
  3. LDAP查詢大小寫是否敏感?

我的控制檯應用程序代碼如下所示:

我覺得我的問題可以在不這塊地回答,但對於那些誰關心我使用測試查詢代碼...

namespace LDAPQueryTester 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      try 
      { 
       string directoryPath = ConfigurationManager.AppSettings["DirectoryPath"]; 
       string searchFilter = ConfigurationManager.AppSettings["SearchFilter"]; 

       DirectoryEntry rootEntry = new DirectoryEntry(directoryPath); 
       DirectorySearcher srch = new DirectorySearcher(rootEntry); 

       srch.SearchScope = SearchScope.Subtree; 

       if (searchFilter.Length > 0) 
       { 
        srch.Filter = searchFilter; 
       } 

       SearchResultCollection res = srch.FindAll(); 

       if (res.Count <= 0) 
       { 
        Console.WriteLine("Your query did NOT return results"); 
       } 
       else 
       { 
        Console.WriteLine("Your query returned results"); 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
       Console.WriteLine(ex.StackTrace); 
      } 

      Console.ReadLine(); 
     } 
    } 
} 
+0

我有同樣的問題,任何人都可以幫忙嗎? – Nayef

+1

@Nayef我標記的答案爲我工作。如果這個修補程序不適合你...我建議發佈一個問題給你的問題。 –

+0

我做過了,如果看看它,我會欣賞它。 http://stackoverflow.com/questions/32345306/move-object-to-an-ou-in-active-directory – Nayef

回答

2

至於我記得,在Users是一個通用的容器 - 不是一個OU - 所以你應該試試這個LDAP路徑:

LDAP://CN=Users,DC=ESDEV-DC01,DC=ESDEV,DC=Com 

注意:CN=Users而不是OU=Users

而且LDAP前綴MUST全部大寫

但是,如果你在.NET 3。5或更高版本,我建議查看新的System.DirectoryServices.AccountManagement命名空間,這使得許多事情更容易使用!

您可以使用PrincipalSearcher和「查詢通過例如」主要做你的搜索:

// create your domain context 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "ESDEV.COM", "CN=Users, DC=ESDEV-DC01,DC=ESDEV,DC=Com")) 
{ 
    // define a "query-by-example" principal - here, we search for a UserPrincipal 
    UserPrincipal qbeUser = new UserPrincipal(ctx); 

    // create your principal searcher passing in the QBE principal  
    PrincipalSearcher srch = new PrincipalSearcher(qbeUser); 

    // find all matches 
    foreach(var found in srch.FindAll()) 
    { 
     // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....   
    } 
} 

如果您尚未 - 絕對看MSDN文章Managing Directory Security Principals in the .NET Framework 3.5這很好地說明如何使System.DirectoryServices.AccountManagement中的新功能的最佳使用。或者查看MSDN documentation on the System.DirectoryServices.AccountManagement命名空間。