Solved

Retrieving Active Directory time fields with .net c#

Posted on 2003-12-04
7
964 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 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