Link to home
Start Free TrialLog in
Avatar of jegregory
jegregory

asked on

Retrieving Active Directory time fields with .net c#

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 ?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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#
Avatar of jegregory
jegregory

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)

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"


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