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();

Please help me in applying filter in principlecontext

nimish jain
  • 141
  • 1
  • 13

1 Answers1

0

You can use PrincipalSearcher to search for a particular user.

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   
   } 
}

You can also use UserPrincipal.

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

You can define different IdentityType if you want to search by samAccountName or etc.

smr5
  • 2,593
  • 6
  • 39
  • 66