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

时间:2015-03-13 11:37:33

标签: c# active-directory

我正在尝试在AD中搜索用户,并且只有在他们处于非活动状态时才会在列表框中显示这些用户。 这是我写的代码

 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#开始。

谢谢

1 个答案:

答案 0 :(得分:2)

IsAccountLockedOut对应于&#34;帐户被锁定&#34;在Active Directory帐户属性中。这意味着由于密码尝试次数过多而导致帐户被锁定。

属性中有另一个设置&#34;帐户被禁用&#34;。如果相应人员离开公司,管理员(大型企业环境中的身份管理系统)通常会使用此功能来禁用帐户。因此该帐户不能再使用,但它仍然存在并且适用于SID查找(将在组或ACL中显示为名称)。

问问自己你的意图是什么。什么意思是&#34;不活跃&#34; ?

您可以将此作为起点:

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也不是很准确,但足以检测出旧的&#34; /未使用的帐户。

我尚未检查UserPrinciple.LastLogon中哪一个对应。小心。

我正在编写一个自己的库,用于在AD中进行搜索,该库基于System.DirectoryServices类。这有一些好处:更好的控制和更好的性能。如果准备就绪,我可能会将其公之于众。

相关问题