'AD request with C#
I'm trying to get all users from AD and their passwords expiration dates with the following code, but got the error.
System.InvalidOperationException: 'The Principal object must be persisted before this method can be called.'
The code:
PrincipalContext domain = new PrincipalContext(ContextType.Domain,"AAA","OU=USERS,OU=TEST,DC=AAA,DC=AA, DC=A");
UserPrincipal user = new UserPrincipal(domain);
PrincipalSearcher pS = new PrincipalSearcher(user);
foreach (UserPrincipal result in pS.FindAll())
{
if (result != null && result.DisplayName != null)
{
DirectoryEntry entry = (DirectoryEntry)user.GetUnderlyingObject();
IADsUser native = (IADsUser)entry.NativeObject;
Console.WriteLine(result.DisplayName, native.PasswordExpirationDate);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
