我正在尝试使用.Net中的DirectorySearcher来查询禁用用户。
我使用了一个非常快的列表函数,与这里发布的函数非常相似。Enumerating Large Groups With Active Directory。
我已尝试将过滤器更改为
(&(objectCategory=person)(userAccountControl:1.2.840.113556.1.4.803:=2))
我没有得到任何结果。似乎我不能在这个庄园里使用DirectorySearcher。有没有人做过这样的事。我只需要基本的信息,并喜欢一个轻量级/快速的查询。
发布于 2011-02-04 05:32:58
使用.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 ", "pass@1w0rd01");然后,您将创建一个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命名空间!
https://stackoverflow.com/questions/4891442
复制相似问题