Connect with Expertise | Find Experts, Get Answers, Share Insights

How do I get "lastLogon" from ActiveDirectory

Sargas Atum
 
Posts: n/a
#1: Nov 15 '05
Hi all,

I am trying to get the field "lastLogon" out of ActiveDirectory.
It should be trivial I thought, but it is not, as I have experienced.

private DataRow FillDataRow( DataRow dataRow, PropertyValueCollection
property , string columnName )
{

if( columnName.Equals("Last Login") && property.Value != null )
{
dataRow[columnName] = property[0];
}

}

I dont have any problems to get all string data from
PropertyValueCollection.

But how do I convert property[0] to DateTime. ( Type of the Object is
System.__ComObject ) thats at least what I know about.

I dont find any example on the net.

thx
a.s.


Arild Bakken
 
Posts: n/a
#2: Nov 15 '05

re: How do I get "lastLogon" from ActiveDirectory


The LastLogon property in ActiveDirectory is a large integer. You need to
reference the ActiveDS.dll COM library and use the LargeInteger interface to
get the high and low parts of the 64 bit integer that it is. The value is a
FileTime value which you can then convert to a .NET DateTime object.

Btw: Note that this is no non-replicated property so that the value you get
is the lastLogon handled by that specific domain controller. To get the
actual Lastlogon you would need to query the value from all
domaincontrollers in the domain and use the highest value.

Arild

"Sargas Atum" <atum@gmx.net> wrote in message
news:4007efc2$1@news.piro.net...[color=blue]
> Hi all,
>
> I am trying to get the field "lastLogon" out of ActiveDirectory.
> It should be trivial I thought, but it is not, as I have experienced.
>
> private DataRow FillDataRow( DataRow dataRow, PropertyValueCollection
> property , string columnName )
> {
>
> if( columnName.Equals("Last Login") && property.Value != null )
> {
> dataRow[columnName] = property[0];
> }
>
> }
>
> I dont have any problems to get all string data from
> PropertyValueCollection.
>
> But how do I convert property[0] to DateTime. ( Type of the Object is
> System.__ComObject ) thats at least what I know about.
>
> I dont find any example on the net.
>
> thx
> a.s.
>[/color]


Sargas Atum
 
Posts: n/a
#3: Nov 15 '05

re: How do I get "lastLogon" from ActiveDirectory


Arild Bakken wrote:
[color=blue]
> The LastLogon property in ActiveDirectory is a large integer. You need to
> reference the ActiveDS.dll COM library and use the LargeInteger interface to
> get the high and low parts of the 64 bit integer that it is. The value is a
> FileTime value which you can then convert to a .NET DateTime object.
>
> Btw: Note that this is no non-replicated property so that the value you get
> is the lastLogon handled by that specific domain controller. To get the
> actual Lastlogon you would need to query the value from all
> domaincontrollers in the domain and use the highest value.
>
> Arild
>
> "Sargas Atum" <atum@gmx.net> wrote in message
> news:4007efc2$1@news.piro.net...
>[color=green]
>>Hi all,
>>
>>I am trying to get the field "lastLogon" out of ActiveDirectory.
>>It should be trivial I thought, but it is not, as I have experienced.
>>
>>private DataRow FillDataRow( DataRow dataRow, PropertyValueCollection
>>property , string columnName )
>>{
>>
>>if( columnName.Equals("Last Login") && property.Value != null )
>>{
>>dataRow[columnName] = property[0];
>>}
>>
>>}
>>
>>I dont have any problems to get all string data from
>>PropertyValueCollection.
>>
>>But how do I convert property[0] to DateTime. ( Type of the Object is
>>System.__ComObject ) thats at least what I know about.
>>
>>I dont find any example on the net.
>>
>>thx
>>a.s.
>>[/color]
>
>
>[/color]

Do you have any example how to do it in c#? I found only some examples
in VB, though modified they dont work.

thx
a.s.

Arild Bakken
 
Posts: n/a
#4: Nov 15 '05

re: How do I get "lastLogon" from ActiveDirectory


LargeInteger largeInt = (LargeInteger)objUser.Properties["lastLogon"][0];
Int64 liTicks = largeInt.HighPart * 0x100000000 + largeInt.LowPart;
if(DateTime.MaxValue.Ticks >= liTicks && DateTime.MinValue.Ticks <= liTicks)
{
DateTime dTemp = DateTime.FromFileTime(liTicks);
}

"Sargas Atum" <atum@gmx.net> wrote in message
news:4007ff3a$1@news.piro.net...[color=blue]
> Arild Bakken wrote:
>[color=green]
> > The LastLogon property in ActiveDirectory is a large integer. You need[/color][/color]
to[color=blue][color=green]
> > reference the ActiveDS.dll COM library and use the LargeInteger[/color][/color]
interface to[color=blue][color=green]
> > get the high and low parts of the 64 bit integer that it is. The value[/color][/color]
is a[color=blue][color=green]
> > FileTime value which you can then convert to a .NET DateTime object.
> >
> > Btw: Note that this is no non-replicated property so that the value you[/color][/color]
get[color=blue][color=green]
> > is the lastLogon handled by that specific domain controller. To get the
> > actual Lastlogon you would need to query the value from all
> > domaincontrollers in the domain and use the highest value.
> >
> > Arild
> >
> > "Sargas Atum" <atum@gmx.net> wrote in message
> > news:4007efc2$1@news.piro.net...
> >[color=darkred]
> >>Hi all,
> >>
> >>I am trying to get the field "lastLogon" out of ActiveDirectory.
> >>It should be trivial I thought, but it is not, as I have experienced.
> >>
> >>private DataRow FillDataRow( DataRow dataRow, PropertyValueCollection
> >>property , string columnName )
> >>{
> >>
> >>if( columnName.Equals("Last Login") && property.Value != null )
> >>{
> >>dataRow[columnName] = property[0];
> >>}
> >>
> >>}
> >>
> >>I dont have any problems to get all string data from
> >>PropertyValueCollection.
> >>
> >>But how do I convert property[0] to DateTime. ( Type of the Object is
> >>System.__ComObject ) thats at least what I know about.
> >>
> >>I dont find any example on the net.
> >>
> >>thx
> >>a.s.
> >>[/color]
> >
> >
> >[/color]
>
> Do you have any example how to do it in c#? I found only some examples
> in VB, though modified they dont work.
>
> thx
> a.s.
>[/color]


Sargas Atum
 
Posts: n/a
#5: Nov 15 '05

re: How do I get "lastLogon" from ActiveDirectory


Arild Bakken wrote:
[color=blue]
> LargeInteger largeInt = (LargeInteger)objUser.Properties["lastLogon"][0];
> Int64 liTicks = largeInt.HighPart * 0x100000000 + largeInt.LowPart;
> if(DateTime.MaxValue.Ticks >= liTicks && DateTime.MinValue.Ticks <= liTicks)
> {
> DateTime dTemp = DateTime.FromFileTime(liTicks);
> }
>[/color]
This works exactly as you wrote it down :)

Thx
Have a nice day.
a.s.

Closed Thread