• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1642
  • Last Modified:

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

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.

Regards.
PinWan
0
PINWAN
Asked:
PINWAN
  • 3
  • 2
2 Solutions
 
Bob LearnedCommented:
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) {
   throw;
 } finally {
   if (!(IsNothing(user))) {
     user.Close();
     user.Dispose();
   }
 }
}


Bob
0
 
PINWANAuthor Commented:
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?

Thanks.
PinWan
0
 
Bob LearnedCommented:
lastLogoff should work, but I can't test it.

Example:

"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.

Bob
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ihenryCommented:
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.
0
 
Bob LearnedCommented:
Thanks for the explanation, Henry, I knew there was the lastLogoff property, but I never used it.

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now