How get the logon / logoff time with c# and Active Directory? LDAP? I need of example? Please

Posted on 2005-05-09
Last Modified: 2012-08-13
Hi Experts

How get the users logon / logoff time with c# and Active Directory? LDAP?

I need to get this (last or current) logon/logoff datetime information of each users in my network.

I need of example.
Plattaform: My code will run into Windows 2000 server.

Question by:PINWAN
    LVL 96

    Accepted Solution

    Untested in a while.  I don't have the authority to test this in my current environment.

    Add a reference to Active DS Type Library through the COM tab.

    using ActiveDs;

    public System.DateTime LastLogon(string path)
     DirectoryEntry user;
     try {
       user = new DirectoryEntry(path);
       IADsLargeInteger int64Val = ((IADsLargeInteger)(user.Properties("lastLogon").Value));
       Int64 longInteger = int64Val.HighPart * 4294967296 + int64Val.LowPart;
       return DateTime.FromFileTime(longInteger);
     } catch (Exception ex) {
     } finally {
       if (!(IsNothing(user))) {


    Author Comment

    Hi TheLearnedOne,

    Do you can write of the example of value of variable path? With this, i get of values with my system administrator

    In you method, if i replace "lastLogon" to "lastLogof" will work fine my friend?

    LVL 96

    Expert Comment

    by:Bob Learned
    lastLogoff should work, but I can't test it.


    "CN=<user ID>,OU=Users,DC=wrightpatterson,DC=afmc,DC=ds,DC=af,DC=mil"

    You have to know the structure of your Active Directory tree, otherwise you will have to search through the entire tree, which for me here is a huge pain in the ass.

    LVL 20

    Assisted Solution

    Unfortunately, there's nothing much you can do with lastLogoff attribute as it never gets updated and always returns zero. Also it's very important to know, lastLogon attribute isn't replicated across domains or merely saying its value is varied for each domain. So in order to get the most recent one, you'd have to query every domain in the forest.
    LVL 96

    Expert Comment

    by:Bob Learned
    Thanks for the explanation, Henry, I knew there was the lastLogoff property, but I never used it.

    LVL 20

    Expert Comment

    Hi, any update? I know this sounds confusing but I think Bob's code should get you started :o)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now