2014-09-21 56 views
0

我一直在寻找一种方法来确定是否一个目录的用户的帐户已禁用或没有,但使用PrincipalContext方法:确定是否一个目录的用户的帐户已禁用帐户使用PrincipalContext方法

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

我已经在这里搜索并看到一些人在目录中使用第二种方法:DirectoryEntry和DirectorySearcher方法(How to determine if user account is enabled or disabled)。我不能使用,因为我所做的一切与PrincipalContext

回答

0

您对UserPrincipal多个属性和方法,您可以检查:

  • Enabled:用户的帐户是否已启用
  • IsAccountLockedOut() :返回一个布尔值,该值指定帐户当前是否被锁定。

查看official MSDN documentation on UserPrincipal了解更多详情。

+0

我很惊讶,我错过了这一个。我会测试一下。 – DoomerDGR8 2014-09-21 08:50:37