使用.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
命名空間!
很酷。我仍然被困在LDAP的土地上。如果這會遭受與舊查詢相同的帳戶限制,您是否知道離開? 1000或1500. – hal9000 2011-02-03 22:11:02