title: PrincipalContext for query in Active Directory
body: <p>I want to make a few simple reports from Active Directory. Following discussions, etc. I found that if I use .NET FW 3.5 and up, it is appropriate to use <code>PrincipalContext</code>. I would like to understand principles and what I can do with this new feature (unlike <code>DirectoryEntry</code>).</p>
  
  <p><strong>Code skeleton</strong></p>
  
  <pre><code>// create your domain context
  PrincipalContext ctx = new PrincipalContext(ContextType.Domain, 
      "YOURDOMAIN", "OU=SomeOU,DC=YourCompany,DC=com");
  
  // define a "query-by-example" principal - here, we search for a UserPrincipal 
  // which has a password that will expire in 3 days or less
  UserPrincipal userTemplate = new UserPrincipal(ctx);
  userTemplate.AdvancedSearchFilter.AccountExpirationDate(DateTime.Today.AddDays(3), MatchType.LessThanOrEquals);
  
  // instantiate searcher
  PrincipalSearcher searcher = new PrincipalSearcher(userTemplate);
  
  // enumerate matching users
  foreach (Principal foundPrincipal in searcher.FindAll())
  {
      UserPrincipal foundUser = (foundPrincipal as UserPrincipal);
  
      if (foundUser != null)
      {
          // do something with users found - e.g. send e-mail
      }
  }
  </code></pre>
  
  <p>It is possible by code up add this properties for login to LDAP?: </p>
  
  <ul>
  <li>what LDAP is used (version 2 or 3) </li>
  <li>how set port on which LDAP is running </li>
  <li>how to work if I need SSL connection? (different port, must be special requirements)</li>
  </ul>
  
  <p>Furthermore, can I do with <code>AdvancedSearchFilter</code> this conditions?<br>
  (I found only <code>AccountExpirationDate</code> and <code>AccountLockoutDate</code>) </p>
  
  <ul>
  <li>users password will expire in the near future </li>
  <li>users password has expired </li>
  <li>check if the user's password can expire </li>
  <li>users account expires (account, no password) </li>
  <li>expired users account (account, no password)</li>
  <li>user account not expired</li>
  </ul>