Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Retrieving Active Directory time fields with .net c#

Posted on 2003-12-04
7
Medium Priority
?
991 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
6 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 2000 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

824 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