从 c# 中的组织单位用户列表中获取 Active Directory 的最后登录日期
Get the last login date for Active Directory from a list of organizational unit users in c#
我已经完成了从组织单位读取所有用户的代码,现在我需要能够读取所有这些用户的列表并显示每个用户的上次登录。
这是正确读取我需要的所有用户的代码
private void btn_LastLoginUser_Click(object sender, EventArgs e)
{
GroupUsers();
}
public void GroupUsers()
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "mydomain", "OU=myEmployees,DC=myCompany,DC=com");
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.Enabled = true;
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach (var found in srch.FindAll())
{
lst_Users.Items.Add(found);
}
}
我需要帮助读取该数据,现在显示每个用户的最后登录日期并将其填充到我的列表框中。
FunCoder,
这应该允许您从 DC 获取上次登录日期和时间。它将找到的用户转换为经过身份验证的原则,其中包含许多不同的属性,这些属性可能有助于您将来寻找其他信息。
foreach(var found in srch.FindAll())
{
var auth = found as AuthenticablePrincipal;
if(auth != null)
{
var Name = auth.Name;
var LastLogin = auth.LastLogon;
}
}
我已经完成了从组织单位读取所有用户的代码,现在我需要能够读取所有这些用户的列表并显示每个用户的上次登录。
这是正确读取我需要的所有用户的代码
private void btn_LastLoginUser_Click(object sender, EventArgs e)
{
GroupUsers();
}
public void GroupUsers()
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "mydomain", "OU=myEmployees,DC=myCompany,DC=com");
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.Enabled = true;
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach (var found in srch.FindAll())
{
lst_Users.Items.Add(found);
}
}
我需要帮助读取该数据,现在显示每个用户的最后登录日期并将其填充到我的列表框中。
FunCoder, 这应该允许您从 DC 获取上次登录日期和时间。它将找到的用户转换为经过身份验证的原则,其中包含许多不同的属性,这些属性可能有助于您将来寻找其他信息。
foreach(var found in srch.FindAll())
{
var auth = found as AuthenticablePrincipal;
if(auth != null)
{
var Name = auth.Name;
var LastLogin = auth.LastLogon;
}
}