I have been looking for a way to determine if a directory user's account is disabled or not but using the PrincipalContext
approach:
using (var validatePrincipalContext = GetPrincipalContext())
{
using (var retrievedUserPrincipal = UserPrincipal.FindByIdentity(validatePrincipalContext, directoryUserName))
{
if (retrievedUserPrincipal == null)
{
LogMessage(String.Format("User {0} failed to verify on {1}.", directoryUserName, domainNameV), Severity.Error);
throw new PlatformException(ErrorCode.DomainCredentialsFailed, new Dictionary<string, string>
{
{"ADUserName", directoryUserName},
{"DirectoryIdentifier", domainNameV}
});
}
// Actual validation
if (validatePrincipalContext.ValidateCredentials(directoryUserName, directoryUserPassword))
{
LogMessage(String.Format("User {0} verified successfully on {1}.", directoryUserName, domainNameV), Severity.Info);
return retrievedUserPrincipal.UserPrincipalName;
}
LogMessage(String.Format("User {0} failed to verify on {1}.", directoryUserName, domainNameV), Severity.Info);
return String.Empty;
}
}
I have searched here and see some people using the second approach with directories: The DirectoryEntry and DirectorySearcher approach (How to determine if user account is enabled or disabled). I cannot use that as I have done everything with the PrincipalContext