登录用户状态

时间:2016-09-16 11:53:16

标签: c# asp.net .net active-directory

我们如何以编程方式访问活动目录中的用户状态?特别 活动/离开/锁定等状态。这是建立一个Web快照,以查看所有登录用户及其各自的状态。

到目前为止,我可以搜索特定域下的所有用户,但没有找到他们当前状态的运气。

using (var context = new PrincipalContext(ContextType.Domain, "domain"))
{
    using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
    {
        foreach (var result in searcher.FindAll())
        {
            DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
            Console.WriteLine("First Name: " + de.Properties["givenName"].Value);
            Console.WriteLine("Last Name : " + de.Properties["sn"].Value);
            Console.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value);

        }
    }
}

2 个答案:

答案 0 :(得分:1)

“离开”不是你要从Active Directory中获得的东西。

可以查看以下属性以获取您想要的一些信息: userAccountControl是您想要查看的主要属性。

在LDAP查询中使用(userAccountControl:AND:=2)会找到已禁用的用户。

其他一些有用的属性是:

  • lastLogon(对于“有效”,您必须确定对您有何活动意义)
  • lockoutTime(lockoutTime> 0)表示他们已被锁定。
  • accountExpires for expired users

答案 1 :(得分:1)

您可以从每个工作站和域控制器上的安全日志中收集logon \ logoff用户事件。通过分析此信息,您可以确定当前活动的用户。不幸的是,该解决方案不适用于拥有数千个工作站和数百个DC的企业环境。有时,由于某种原因,安全日志中有时会缺少logon \ logoff事件。

如果使用lastLogon \ lastLogonTimestamp属性,请注意:

  • lastLogon属性不在域控制器之间复制。这是您从中收集信息的域控制器上的最后一次登录用户时间。要确定上次用户登录,您必须从所有DC收集属性并选择最大值。
  • lastLogonTimestamp在9-14天内复制一次。 lastLogontimeStamp将比当前日期晚9-14天。