获取Active Directory组的成员并检查它们是否处于活动状态

问题描述:

我正在尝试搜索AD中的用户,并仅在他们处于非活动状态时才将其显示在列表框中。 这是代码我已经写获取Active Directory组的成员并检查它们是否处于活动状态

private void button1_Click(object sender, EventArgs e) 
    { 
     PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Domain, "DX", "DC=RX,DC=PX,DC=com"); 

     ListUser(insPrincipalContext); 

    } 
private void ListUser(PrincipalContext insPrincipalContext) 
    { 
     UserPrincipal insUserPrincipal = new UserPrincipal(insPrincipalContext); 
     insUserPrincipal.Name = "*"; 
     SearchUsers(insUserPrincipal); 
    } 
private void SearchUsers(UserPrincipal parUserPrincipal) 
    { 
     listBox1.Items.Clear(); 
     PrincipalSearcher insPrincipalSearcher = new PrincipalSearcher(); 
     insPrincipalSearcher.QueryFilter = parUserPrincipal; 
     PrincipalSearchResult<Principal> results = insPrincipalSearcher.FindAll(); 
     foreach (Principal p in results) 
     { 

      UserPrincipal theUser = p as UserPrincipal; 
      if (theUser != null) 
      { 
       if (***theUser.IsAccountLockedOut()***)//**Is this same as Active or Inactive?** 
       { 
        listBox1.Items.Add(p); 
       } 
       else 
       { 

       } 
      } 
     } 
    } 

所以我的问题是是否(theUser)IsAccountLockedUp是一样询问用户是无效的? 我知道有人可能会建议这个问题是Get members of Active Directory Group and check if they are enabled or disabled的副本,但这里的问题是我没有测试用户测试,我只是从C#开始。

谢谢

IsAccountLockedOut对应于Active Directory帐户属性“帐户被锁定”。这意味着由于密码尝试次数过多,帐户被锁定。

属性“Account is disabled”中还有另一个设置。管理员(大型企业环境中的身份管理系统)常常使用此功能,以便在相应人员离开公司时禁用该帐户。因此该帐户无法再使用,但它仍然存在,并可用于SID查找(将显示为组或ACL中的名称)。

问问自己你的意图是什么。 “不活跃”意味着什么?

你或许可以以此为出发点:

if (theUser != null) 
{ 
    if (theUser.IsAccountLockedOut()) 
    { 
     // account locked out 
    } 

    // Enabled is nullable bool 
    if (!(theUser.Enabled == true)) 
    { 
     // account disabled 
    } 

    if (theUser.LastLogon == null || 
     theUser.LastLogon < DateTime.Now - TimeSpan.FromDays(150)) 
    { 
     // never logged on or last logged on long time ago 
     // see below in the text for important notes ! 
    } 
} 

检查LastLogon可用于查找孤立帐户。但要知道,一个妇女可能在产假:-)

重要

在Active Directory中,有两个类似的属性:

  • LastLogon
  • 的lastLogonTimestamp

请参阅

http://blogs.technet.com/b/askds/archive/2009/04/15/the-lastlogontimestamp-attribute-what-it-was-designed-for-and-how-it-works.aspx

为区别。简而言之:只有LastLogonTimeStamp属性在域控制器之间复制,并且可以安全地用于检查孤立的帐户。但即使LastLogonTimeStamp也不准确,但足以检测“旧”/未使用的帐户。

我还没有检查其中哪个与UserPrinciple.LastLogon对应。小心。

我正在编写一个自己的库在AD中进行搜索的过程,它基于System.DirectoryServices类。这有一些好处:更好的控制和更好的性能。如果准备好了,我可能会公开。