[英]How to get the “LastLogon” time stamp for users from Active Directory
I am creating an app to display the users last logon time stamp as well as date created and a few other info. 我正在创建一个应用程序来显示用户上次登录时间戳以及创建日期和其他一些信息。 How do I get the lastlogon to display in my listview?
如何在listview中显示lastlogon?
I have tried various solutions on SO as well as various other websites but none seems to be working 我已经在SO以及其他各种网站上尝试了各种解决方案,但似乎都没有
This is the code I am using to display the information needed 这是我用来显示所需信息的代码
lvwListView.Clear();
lvwListView.Columns.Add("Name", 175, HorizontalAlignment.Left);
lvwListView.Columns.Add("LanID", 100, HorizontalAlignment.Left);
lvwListView.Columns.Add("Email", 225, HorizontalAlignment.Left);
lvwListView.Columns.Add("Phone Number", 150, HorizontalAlignment.Left);
lvwListView.Columns.Add("When Created", 150, HorizontalAlignment.Left);
lvwListView.Columns.Add("Last Logon", 150, HorizontalAlignment.Left);
if (results != null)
{
foreach (Principal p in results)
{
itmListItem = new ListViewItem();
itmListItem.Text = p.Name;
itmListItem.SubItems.Add(p.SamAccountName);
if (p.StructuralObjectClass == "user")
{
var uP = (UserPrincipal)p;
if (uP != null)
{
itmListItem.SubItems.Add(uP.EmailAddress);
}
}
var creationDate = string.Empty;
var telephoneNumber = string.Empty;
var lastLogon = string.Empty;
var prop = string.Empty;
var directoryEntry = p.GetUnderlyingObject() as DirectoryEntry;
prop = "whenCreated";
if (directoryEntry.Properties.Contains(prop))
{
creationDate = directoryEntry.Properties[prop].Value.ToString();
}
prop = "telephoneNumber";
if (directoryEntry.Properties.Contains(prop))
{
telephoneNumber = directoryEntry.Properties[prop].Value.ToString();
}
prop = "lastLogon";
if (directoryEntry.Properties.Contains(prop))
{
lastLogon = directoryEntry.Properties[prop].Value.ToString();
}
itmListItem.SubItems.Add(telephoneNumber);
itmListItem.SubItems.Add(creationDate);
itmListItem.SubItems.Add(lastLogon);
lvwListView.Items.Add(itmListItem);
lvwListView.Refresh();
itmListItem = null;
this.Text = Application.ProductName + " (" + groupName + ")";
}
ctx.Dispose();
}
The code above displays the Name
, LanId
, Email
, phone number
and when created
perfectly, but for the lastlogon
it displays System._ComObject
. 上面的代码显示
Name
, LanId
, Email
, phone number
以及完美when created
,但对于lastlogon
它显示System._ComObject
。
I don't fully understand why it will display the WhenCreated
but not the lastLogon
? 我不完全理解为什么它会显示
WhenCreated
而不是lastLogon
?
WhenCreated is stored as String (Generalized-Time) type , whereas Last-Logon is stored as Interval syntax , which is convertible to an IADsLargeInteger . WhenCreated存储为String(Generalized-Time)类型 ,而Last-Logon存储为Interval语法 ,可以转换为IADsLargeInteger 。
Here's an example extension that will convert that to a familiar System.DateTime
: 这是一个示例扩展,将其转换为熟悉的
System.DateTime
:
public static class ActiveDirectoryExtensions
{
public static DateTime GetLastLogon(this DirectoryEntry entry)
{
var activeDirectoryLargeInteger = (IAdsLargeInteger)entry.Properties["lastLogon"].Value;
return DateTime.FromFileTimeUtc((activeDirectoryLargeInteger.HighPart << 32) + activeDirectoryLargeInteger.LowPart);
}
[ComImport, Guid("9068270b-0939-11d1-8be1-00c04fd8d503"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
internal interface IAdsLargeInteger
{
long HighPart { get; set; }
long LowPart { get; set; }
}
}
Usage: 用法:
var lastLogon = directoryEntry.GetLastLogon()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.