I am trying to work with active directory to get users information. For the most part, it's working. The problem is I cant seem to get a users last logon date. Anyone have any suggestions? My code is as follows:
Public Shared Function GetUsersByUsername(ByVal Username As String) As ADUser
Dim myUser As New ADUser
Dim oroot As DirectoryEntry = New DirectoryEntry("GC://ldap.myCompany.com")
Dim osearcher As DirectorySearcher = New DirectorySearcher(oroot)
Dim result As SearchResult
osearcher.Filter = String.Format("(&(SAMAccountName={0}))", Username)
osearcher.PropertiesToLoad.Add("cn")
osearcher.PropertiesToLoad.Add("SAMAccountName") 'Users login name
osearcher.PropertiesToLoad.Add("givenName") 'Users first name
osearcher.PropertiesToLoad.Add("sn") 'Users last name
osearcher.PropertiesToLoad.Add("mail") 'Email address
osearcher.PropertiesToLoad.Add("accountExpires") 'expiration date
result = osearcher.FindOne
Try
Dim userPath As String() = result.Path.ToString.Split(New Char() {","c})
Dim parsedString As String
Dim User As DirectoryEntry
Dim expirationDate As String
User = result.GetDirectoryEntry()
myUser.UserID = result.Properties("cn").Item(0)
myUser.EmailAddress = result.Properties("mail").Item(0)
myUser.FirstName = result.Properties("givenName").Item(0)
myUser.LastName = result.Properties("sn").Item(0)
expirationDate = result.Properties("accountExpires").Item(0)
If (isAccountLocked(User) = True) Then
myUser.Status = ADUser.AccountStatus.Locked
ElseIf (isAccountEnabled(User) = False) Then
myUser.Status = ADUser.AccountStatus.Disabled
ElseIf (isAccountExpired(expirationDate) = True) Then
myUser.Status = ADUser.AccountStatus.Expired
Else
myUser.Status = ADUser.AccountStatus.Active
End If
parsedString = userPath((userPath.Length - 3))
myUser.Domain = parsedString.Substring(3, parsedString.Length - 3)
Catch ex As Exception
Return Nothing
End Try
Return myUser
End Function