Solved

Retrieving Active Directory time fields with .net c#

Posted on 2003-12-04
7
957 Views
Last Modified: 2008-03-17
I an working on an Intranet where I need to produce reports from Active Directory. I have no problem accessing and retrieving the AD data I need but have a problem converting lastLogon  to a real date/time. Apparently this is stored as a high part and a low part so needs HighPart and LowPart methods to break the number into two 32-bit components. I am going round in circles trying to find out how to do this in c#. Does anyone there have code that works ?
0
Comment
Question by:jegregory
  • 4
  • 2
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9874105
I know that you need it for C#, but I found this for VBScript:

Recipe 6.27: Determining a User's Last Logon Time
http://www.ondotnet.com/pub/a/dotnet/excerpt/ADcookbook_chap1/?page=2

' This code prints the last logon timestamp for a user.
' ------ SCRIPT CONFIGURATION ------
strUserDN = "<UserDN>"  ' e.g. cn=user,ou=department,dc=server,dc=com
' ------ END CONFIGURATION ---------
 
set objUser =  GetObject("LDAP://" & strUserDN)
set objLogon = objUser.Get("lastLogonTimestamp")
intLogonTime = objLogon.HighPart * (2^32) + objLogon.LowPart
intLogonTime = intLogonTime / (60 * 10000000)
intLogonTime = intLogonTime / 1440
WScript.Echo "Approx last logon timestamp: " & intLogonTime + #1/1/1601#
0
 

Author Comment

by:jegregory
ID: 9874219
Thanks, I'd found that one but I can't tranlate it into c# ! The problem is I can't find a c# class that will do a high part, low part split.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 9874939
This works for me, since I only have on DC.  If you have multiple DCs you will have to query each DC.

Add Reference to System.DirectoryServices + COM Active DS Type Library.

using System.DirectoryServices;
using ActiveDs;

      public string LastUserLogin()
      {

         DirectorySearcher mySearch;
         SearchResult myResult;

         mySearch = new DirectorySearcher("cn=<User name goes here>");

         myResult = mySearch.FindOne();

         DirectoryEntry myEntry = new DirectoryEntry(myResult.Path);

         IADsLargeInteger li = (IADsLargeInteger) myEntry.Properties["lastLogon"][0];

         long fileDate = li.HighPart * 4294967296 + li.LowPart;
         
         string returnDate = DateTime.FromFileTime(fileDate).ToString() ;

         return returnDate;

      }

This was an interesting challenge since I don't use C# very much.  

For those who care, here is the VB.NET code that I started with:

      Dim mySearch As New DirectorySearcher("cn=<User name goes here>")
      Dim myResult As SearchResult = mySearch.FindOne

      Dim myEntry As DirectoryEntry = New DirectoryEntry(myResult.Path)
      Dim fileDate As Long

      fileDate = (myEntry.Properties("lastLogon").Item(0).HighPart * 4294967296) + _
         (myEntry.Properties("lastLogon").Item(0).LowPart)

      Return DateTime.FromFileTime(fileDate).ToString
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:jegregory
ID: 9881863
When I compile this I get 'The type or namespace ActiveDS could not be found'. I think this is because ActiveDS is not part of the .net framework, but part of ADSI. So how do I link in the ADSI bit - is there a dll I need ?

I am building this as part of an Intranet, so using web forms not Windows forms and accessing Active directory remotely from within the browser.

(I am using Dreamweaver for editing and the command line compiler -  not Visual Studio)

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9882000
I have this in my .csproj file in the References section:

                <Reference
                    Name = "ActiveDs"
                    Guid = "{97D25DB0-0363-11CF-ABC4-02608C9E7553}"
                    VersionMajor = "1"
                    VersionMinor = "0"
                    Lcid = "0"
                    WrapperTool = "tlbimp"


0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10246772
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: TheLearnedOne {http:#9874939}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

14 Experts available now in Live!

Get 1:1 Help Now