Finding the last logon date time

Mar 8, 2010 at 11:02 AM
Edited Mar 8, 2010 at 11:23 AM

Hi,

I was in need to find the last logon date/time and noticed that it was a little more complex to get, so thought I'd give a solution if anybody was interested

        Int64 lastLogonThisServer = new Int64();
        [DirectoryAttribute("lastLogon")]
        public object LastLogon
        {
            get { return lastLogonThisServer; }
            set
            {
                IADsLargeInteger lgInt = (IADsLargeInteger)value;
                lastLogonThisServer = ((long)lgInt.HighPart << 32) + lgInt.LowPart;
            }
        }

        public DateTime readableLastLogon 
        {
            get { return DateTime.FromFileTime((Int64)LastLogon); }
        }

Oct 9, 2010 at 5:12 AM

Thanks. Very helpful.

Nov 4, 2010 at 4:21 AM

With the obvious caveat that this only gets you the last logon date against whichever domain controller your query was executed.

Ref, e.g., http://blogs.dirteam.com/blogs/jorge/archive/2008/02/10/showing-last-logon-info-at-logon-in-windows-server-2008.aspx