PrincipalContext to query in Active Directory

I want to do some simple reports from Active Directory. After discussions, etc. I found that if I am using .NET FW 3.5 and above it makes sense to use PrincipalContext

. I would like to understand the principles and what I can do with this new feature (as opposed to DirectoryEntry

).

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

      

Can I add code to add this property for LDAP login ?:

  • which LDAP is being used (version 2 or 3)
  • how to set the port on which LDAP is running
  • How do I work if I need an SSL connection? (there must be special requirements for different ports)

Also, can I do with AdvancedSearchFilter

these conditions?
(I only found AccountExpirationDate

and AccountLockoutDate

)

  • user password will expire soon
  • the user's password has expired
  • check user password expiration date
  • user account expires (account, no password)
  • expired user account (account, no password)
  • User account has not expired
+3


source to share


1 answer


sorry for the late reply. In the solution I found these two links which describe all the information. Just like it is only needed to combine with the above code.

get the "Minimum password length" value in the domain password policy



Derek's House - Password Expiration Email Utility

0


source







All Articles