2012-02-26 20 views
9

當我運行此查詢的DirectorySearcher過濾

// Next row is used to login to AD 
DirectoryEntry entry = GetEntry(domain, adminUser, adminPassword); 
// Here starts the query 
DirectorySearcher search = new DirectorySearcher(entry) 
{ 
    SearchScope = SearchScope.Subtree, 
    Filter = "(&" + 
     "(objectClass=user)" + 
     // "(distinguishedname=*OU=Ingegneria*)" + 
     "(givenname=s*)" + 
     "(samaccountname=*100)" + 
    ")" 
}; 
search.PropertiesToLoad.Add("distinguishedname"); 
SearchResultCollection result = search.FindAll(); 

我得到六個條目,這就是正確的。
所有記錄,如果我用record.GetDirectoryEntry()

distinguishedname: CN=xxx,OU=Utenti,OU=Ingegneria,DC=xxx,DC=xxx 

無論如何,如果我在過濾器的distinguishedname部分刪除評論,我得到零項!
我也試圖使用search.PropertiesToLoad.Add("distinguishedname");沒有運氣。
如何在過濾器中搜索distinguishedname

UPDATE:
如果我嘗試使用過濾器"(distinguishedname=*)" +,我仍然得到六個記錄,所以我想我可以在搜索的distinguishedName ...
UPDATE2:
我還試圖用代碼在Search Active Directory for an OU using a partial path to the OU

Filter = "(&(objectClass=user)(ou=Ingegneria))"; 

但我有零項(我有兩個,如果我刪除(objectClass=user)部分)

+0

嘗試刪除'OU = INGEGNERIA *',看看會發生什麼 – Shai 2012-02-26 08:39:40

+0

@Shai:我需要的過濾器的那部分......無論如何看看我的更新。 – Marco 2012-02-26 08:46:44

+0

似乎是http://stackoverflow.com/questions/5872838/search-active-directory-for-an-ou-using-a-partial-path-to-the-ou的重複 - 這可能會幫助您搜索由OU。 – 2012-02-26 09:31:32

回答

23

如果你想查詢只是那麼你應該綁定到容器在初始連接:

// Next row is used to login to AD 
string ldapPath = "LDAP://OU=Ingegneria,DC=xxx,DC=xxx"; 
DirectoryEntry searchRoot = GetEntry(ldapPath, adminUser, adminPassword); 

// Here starts the query 
DirectorySearcher search = new DirectorySearcher(searchRoot) 
{ 
    SearchScope = SearchScope.Subtree, 
    Filter = "(&" + 
     "(objectClass=user)" + 
     "(givenname=s*)" + 
     "(samaccountname=*100)" + 
    ")" 
}; 

search.PropertiesToLoad.Add("distinguishedname"); 
SearchResultCollection result = search.FindAll(); 

這樣的話,你也大量減少空間公元需要進行搜索,從而加快您的搜索。

如果你正在使用.NET 3.5或更高版本,可以使用PrincipalSearcher和「查詢通過例如」主要做你的搜索:

// create your domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "OU=Ingegneria,DC=xxx,DC=xxx"); 

// define a "query-by-example" principal - here, we search for a UserPrincipal 
UserPrincipal qbeUser = new UserPrincipal(ctx); 
qbeUser.GivenName = "s*"; 
qbeUser.SamAccountName = "*100"; 

// 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" 
    UserPrincipal userFound = found as UserPrincipal; 

    if(userFound != null) 
    { 
     // do something with your user principal here.... 
    } 
} 

如果您還沒有 - 絕對看MSDN文章Managing Directory Security Principals in the .NET Framework 3.5這很好地說明如何使新功能的最佳使用System.DirectoryServices.AccountManagement

+0

非常感謝你的回答,它確實是完整的。我會盡快看一看。我唯一的疑問是,要搜索的OU是動態的(是運行時用戶定義的過濾器的一部分)......但我可能找到一種方法來管理它。謝謝 – Marco 2012-02-26 09:58:21

+0

只是爲了完成:是否有一種方法來搜索綁定AD根的OU(或在我的問題中的'distinguishedname'的一部分)? – Marco 2012-02-26 10:04:44

+0

@Marco:我真的不知道,對不起 - 從來沒有嘗試過我自己..... – 2012-02-26 10:21:17