Results 1 to 4 of 4

Thread: [RESOLVED] Getting account expiration date from Active Directory for a given user

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    54

    Resolved [RESOLVED] Getting account expiration date from Active Directory for a given user

    I'm need to create a function that gets the account expiration date from Active Directory for a given user. I've been a VB developer for years, but I'm not that familiar with accssing AD information and I'm not seeing a lot of documentation out there. Any help or code samples would be greatly appreciated.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Getting account expiration date from Active Directory for a given user

    Few comments regarding your request:

    - user must have admin rights on the remote server
    - you may try using NetUserGetInfo along with USER_INFO_3 structure but I'm not sure about the exp. date.

    Here is a sample you may try:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const ERROR_SUCCESS As Long = 0
    4.  
    5. Private Type USER_INFO_3
    6.    usri3_name              As Long
    7.    usri3_password          As Long
    8.    usri3_password_age      As Long
    9.    usri3_priv              As Long
    10.    usri3_home_dir          As Long
    11.    usri3_comment           As Long
    12.    usri3_flags             As Long
    13.    usri3_script_path       As Long
    14.    usri3_auth_flags        As Long
    15.    usri3_full_name         As Long
    16.    usri3_usr_comment       As Long
    17.    usri3_parms             As Long
    18.    usri3_workstations      As Long
    19.    usri3_last_logon        As Long
    20.    usri3_last_logoff       As Long
    21.    usri3_acct_expires      As Long
    22.    usri3_max_storage       As Long
    23.    usri3_units_per_week    As Long
    24.    usri3_logon_hours       As Long
    25.    usri3_bad_pw_count      As Long
    26.    usri3_num_logons        As Long
    27.    usri3_logon_server      As Long
    28.    usri3_country_code      As Long
    29.    usri3_code_page         As Long
    30.    usri3_user_id           As Long
    31.    usri3_primary_group_id  As Long
    32.    usri3_profile           As Long
    33.    usri3_home_dir_drive    As Long
    34.    usri3_password_expired  As Long
    35. End Type
    36.  
    37. Private Declare Function NetUserGetInfo Lib "Netapi32" _
    38.     (servername As Byte, username As Byte, _
    39.     ByVal level As Long, bufptr As Long) As Long
    40.  
    41. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    42.     (pTo As Any, uFrom As Any, ByVal lSize As Long)
    43.  
    44. Private Sub Command1_Click()
    45. Dim success As Long
    46. Dim buff As Long
    47. Dim bServer() As Byte
    48. Dim bUser() As Byte
    49. Dim ui3 As USER_INFO_3
    50. Dim lPwdDaysSoFar As Long
    51.  
    52.     bUser = "validuser" & vbNullChar '<<--- provide valid user
    53.     bServer = "\\some.domain.info" & vbNullChar '<<--- provide valid domain
    54.    
    55.     'retrieve the user info into a buffer
    56.     success = NetUserGetInfo(bServer(0), bUser(0), 3, buff)
    57.    
    58.     If success = ERROR_SUCCESS Then
    59.        
    60.         'copy it to a USER_INFO_3 structure
    61.         CopyMemory ui3, ByVal buff, Len(ui3)
    62.        
    63.         Debug.Print ui3.usri3_acct_expires      'true/false
    64.         Debug.Print ui3.usri3_password_expired  'true/false
    65.         Debug.Print ui3.usri3_password_age      'long (most probably seconds ellapsed since the last change)
    66.        
    67.         lPwdDaysSoFar = ui3.usri3_password_age \ 86400
    68.         Debug.Print "Days since pwd last changed: " & lPwdDaysSoFar
    69.        
    70.     End If
    71.  
    72. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    54

    Re: Getting account expiration date from Active Directory for a given user

    Thank you. I'll play around with this code and post back when I have time.

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    54

    Resolved SOLVED Re: Getting account expiration date from Active Directory for a given user

    Here's what I ended up using:

    VB Code:
    1. Public Function days_until_password_expires(ByVal strDomain As String, ByVal strusername As String) As Integer
    2.         Dim dblMaxPwdNano As Double
    3.         Dim dblMaxPwdSecs As Double
    4.         Dim dblMaxPwdDays As Double
    5.         Dim sDomainAndUserName As String = strDomain & "\" & strusername
    6.  
    7.         Dim oDomain As DirectoryEntry = New DirectoryEntry("LDAP://mesaverde", adminusername, adminpwd")
    8.         Dim obj As Object = oDomain.NativeObject
    9.         Dim search As DirectorySearcher = New DirectorySearcher(oDomain)
    10.         search.Filter = "(SAMAccountName=" & strusername & ")"
    11.         search.PropertiesToLoad.Add("cn")
    12.         Dim result As SearchResult = search.FindOne()
    13.         If Not IsDBNull(result) Then
    14.             Dim oUser As DirectoryEntry = result.GetDirectoryEntry
    15.             If Not IsNothing(oUser.Properties("userAccountControl").Value) Then
    16.  
    17.  
    18.                 '***********************************************************************************
    19.                 ' obtain the maximum password age
    20.                 Const ONE_HUNDRED_NANOSECOND = 0.0000001
    21.                 Const SECONDS_IN_DAY = 86400
    22.                 Dim objMaxPwdAge As ActiveDs.LargeInteger = oDomain.Properties("MaxPwdAge").Value
    23.                 If objMaxPwdAge.LowPart = 0 Then
    24.                     'MsgBox("The Maximum Password Age is set to 0 in the domain. Therefore, the password does not expire.")
    25.                 Else
    26.                     dblMaxPwdNano = System.Math.Abs(objMaxPwdAge.HighPart * 2 ^ 32 + objMaxPwdAge.LowPart)
    27.                     dblMaxPwdSecs = dblMaxPwdNano * ONE_HUNDRED_NANOSECOND   ' LINE 13
    28.                     dblMaxPwdDays = Int(dblMaxPwdSecs / SECONDS_IN_DAY)      ' LINE 14
    29.                     'MsgBox("Maximum password age: " & dblMaxPwdDays & " days")
    30.                 End If
    31.  
    32.  
    33.  
    34.                 ''PasswordLastChanged
    35.                 Dim liPasswdLastSet As ActiveDs.LargeInteger = oUser.Properties("pwdLastSet").Value
    36.  
    37.                 ''Convert the highorder/loworder parts to a long
    38.                 Dim datePasswdLastSet As Long = ((CType((liPasswdLastSet.HighPart) * 2 ^ 32, Long)) + CType(liPasswdLastSet.LowPart, Long))
    39.  
    40.                 ''Now convert it from FileTime to DateTime string
    41.                 Dim datePasswdLastSetVal As DateTime = DateTime.FromFileTime(datePasswdLastSet)
    42.                 Dim datepwdexpire As DateTime = DateValue(datePasswdLastSetVal.AddDays(dblMaxPwdDays))
    43.                 Dim ts As TimeSpan = datepwdexpire.Subtract(Now)
    44.  
    45.                 Return ts.Days
    46.  
    47.             End If
    48.         End If
    49.     End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width