1
DirectorySearcher deSearch;SearchResultCollection result;
deSearch.SearchRoot = baseResult.GetDirectoryEntry();// I know this one can be done like - PrincipalContext pContext = new PrincipalContext(ContextType.Domain, deSearch.SearchRoot.Path);
deSearch.Filter = "(&(&(objectClass=user)(objectCategory=person))(name=" + name + "))"; //???? **Not sure how to apply the filter in Principal Context**
results = deSearch.FindAll();

请帮助我在原则上下文中应用过滤器

4

1 回答 1

0

您可以使用它PrincipalSearcher来搜索特定用户。

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{ 
   UserPrincipal searchUser = new UserPrincipal(ctx);
   searchUser.GivenName = "Name";

   PrincipalSearcher srch = new PrincipalSearcher(searchUser);
   foreach(var found in srch.FindAll())
   {
     //found will contain the info   
   } 
}

您也可以使用UserPrincipal.

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
   using (UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.DistinguishedName, "DN"))
   if (user != null)
   {
      //user contains info
   }
}

IdentityType如果您想按samAccountNameor 等​​搜索,您可以定义不同的。

于 2016-04-10T18:12:15.293 回答