2011-02-03 57 views
2

我正在嘗試使用.Net中的DirectorySearcher查詢禁用用戶。查詢禁用帳戶的ADAM/ADLDS

我使用了一個相當快速的列表功能,與此處發佈的功能非常相似。 Enumerating Large Groups With Active Directory

我試圖改變過濾器

(&(objectCategory=person)(userAccountControl:1.2.840.113556.1.4.803:=2))

我沒有得到任何結果。看來我不能在這個莊園中使用DirectorySearcher。有沒有人做過這樣的事情?我只需要基本的信息,並希望輕量級/快速查詢。

回答

3

使用.NET 3.5中引入的System.DirectoryServices.AccountManagement名稱空間,類似的事情變得更容易。

閱讀所有關於它在這裏:Managing Directory Security Principals in the .NET Framework 3.5

您必須首先建立上下文爲您的操作 - AD LDS明確支持:

// create a context for an AD LDS store pointing to the 
// partition root using the credentials for a user in the AD LDS store 
// and SSL for encryption 
PrincipalContext ldsContext = new PrincipalContext(
    ContextType.ApplicationDirectory, "sea-dc-02.fabrikam.com:50001", 
    "ou=ADAM Users,o=microsoft,c=us", 
    ContextOptions.SecureSocketLayer | ContextOptions.SimpleBind, 
    "CN=administrator,OU=ADAM Users,O=Microsoft,C=US ", "[email protected]"); 

,然後你需要創建一個PrincipalSearcher和定義在一個「查詢範例」風格你正在尋找什麼:

// create a principal object representation to describe 
// what will be searched 
UserPrincipal user = new UserPrincipal(ldsContext); 

// define the properties of the search (this can use wildcards) 
user.Enabled = false; 
user.Name = "user*"; 

// create a principal searcher for running a search operation 
PrincipalSearcher pS = new PrincipalSearcher(); 

// assign the query filter property for the principal object you created 
// you can also pass the user principal in the PrincipalSearcher constructor 
pS.QueryFilter = user; 

// run the query 
PrincipalSearchResult<Principal> results = pS.FindAll(); 

Console.WriteLine("Disabled accounts starting with a name of 'user':"); 
foreach (Principal result in results) 
{ 
    Console.WriteLine("name: {0}", result.Name); 
} 

很漂亮,呃?如果你可以 - 使用新的S.DS.AM命名空間!

+0

很酷。我仍然被困在LDAP的土地上。如果這會遭受與舊查詢相同的帳戶限制,您是否知道離開? 1000或1500. – hal9000 2011-02-03 22:11:02