Solved

Retrieving Active Directory time fields with .net c#

Posted on 2003-12-04
7
955 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
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

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

13 Experts available now in Live!

Get 1:1 Help Now