How do I get "lastLogon" from ActiveDirectory

  • Thread starter Thread starter Sargas Atum
  • Start date Start date
S

Sargas Atum

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.
 
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
 
Arild said:
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

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.

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.
 
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 said:
Arild said:
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

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.

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 said:
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);
}
This works exactly as you wrote it down :)

Thx
Have a nice day.
a.s.
 
Back
Top