Solved

Retrieving Active Directory time fields with .net c#

Posted on 2003-12-04
7
970 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 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