oklahomawebhost
06-16-2006, 01:54 PM
Hello I am trying to dive into asp.net development, and I've been given the task of re-using some old code and placeing it into a site. The problem is I have no idea how to paste the code to get it to work. I have very strong background in PHP development, but asp.net (vb.net) is a completely different monster for me. It's 2 pieces of code, I believe one is the function, the other executes it?
This is the first code:
objNavigation is an object we use for user info, but you can get the User object by going to Page.User, I believe.
The exception stuff is one of our classes as well.
The age and maximum age are returned in seconds.
'
'******************************************************************************************'
' Method to retrieve a user's password time to expire.
'
' Input: No input.
' Output is supplied through a return value of integer.
'******************************************************************************************'
'
Protected Function GetPasswordTimeToExpire() As Integer
Try
'Get the user information.
Dim strUser As String = objNavigation.User.Identity.Name.Replace("\"c, "/"c)
'Impersonate the user to access AD.
Dim objImpersonatedUser As WindowsImpersonationContext = _
CType(objNavigation.User.Identity, WindowsIdentity).Impersonate()
'Create a new user object
Dim objUser As New DirectoryEntry("WinNT://" & strUser & ",User)
'Determine the age of the password, and the max age (expire time)
Dim intPasswordAge As Integer = _
Convert.ToInt32(objUser.Properties.Item("PasswordAge").Item(0))
Dim intMaxAge As Integer = _
Convert.ToInt32(objUser.Properties.Item("MaxPasswordAge").Item(0))
'Close the connection to AD.
objUser.Close()
'Undo the impersonation
objImpersonatedUser.Undo()
'Calculate and return the password time to expire
Return intMaxAge - intPasswordAge
Catch e1 As Exception
'Log the generic error
Dim objGenericException As New RevenueManagementData.clsGenericException
objGenericException.ExceptionError = e1
objGenericException.ErrorSource = strSource
objGenericException.LogGenericException()
strResult = objGenericException.Result
Return 0
End Try
End Function
This is the second bit of code that actually uses the GetPasswordTimeToExpire function.
'Get the number of seconds till password expires...
Dim intSecondsTillExpire As Integer = GetPasswordTimeToExpire()
'If error getting password time to expire, print message and exit
If strResult.Length > 0 Then
Listbox_Bad(strResult, True)
Exit Sub
End If
'Calculate the number of days till the password expires
Dim tsTimeTillExpire As TimeSpan = TimeSpan.FromSeconds(intSecondsTillExpire)
Dim intDaysTillExpire As Integer = tsTimeTillExpire.Days
'If there is less than one day till the password expires, set the message to hours
'Otherwise, set it to days
If intDaysTillExpire = 0 Then
'Calculate the number of hours till the password expires
Dim intHoursTillExpire As Integer = tsTimeTillExpire.Hours
'Display message
lblPasswordWarning.Text = "Your password will expire in " & intHoursTillExpire & _
" hours!"
Else
'Display message
lblPasswordWarning.Text = "Your password will expire in " & intDaysTillExpire & _
" days."
End If
'If the password will expire in the next 7 days, make the warning more noticable
If intDaysTillExpire <= 7 Then
lblPasswordWarning.CssClass = "TextBoxBad"
End If
Do I just paste these into a .aspx file and call it done?
This is the first code:
objNavigation is an object we use for user info, but you can get the User object by going to Page.User, I believe.
The exception stuff is one of our classes as well.
The age and maximum age are returned in seconds.
'
'******************************************************************************************'
' Method to retrieve a user's password time to expire.
'
' Input: No input.
' Output is supplied through a return value of integer.
'******************************************************************************************'
'
Protected Function GetPasswordTimeToExpire() As Integer
Try
'Get the user information.
Dim strUser As String = objNavigation.User.Identity.Name.Replace("\"c, "/"c)
'Impersonate the user to access AD.
Dim objImpersonatedUser As WindowsImpersonationContext = _
CType(objNavigation.User.Identity, WindowsIdentity).Impersonate()
'Create a new user object
Dim objUser As New DirectoryEntry("WinNT://" & strUser & ",User)
'Determine the age of the password, and the max age (expire time)
Dim intPasswordAge As Integer = _
Convert.ToInt32(objUser.Properties.Item("PasswordAge").Item(0))
Dim intMaxAge As Integer = _
Convert.ToInt32(objUser.Properties.Item("MaxPasswordAge").Item(0))
'Close the connection to AD.
objUser.Close()
'Undo the impersonation
objImpersonatedUser.Undo()
'Calculate and return the password time to expire
Return intMaxAge - intPasswordAge
Catch e1 As Exception
'Log the generic error
Dim objGenericException As New RevenueManagementData.clsGenericException
objGenericException.ExceptionError = e1
objGenericException.ErrorSource = strSource
objGenericException.LogGenericException()
strResult = objGenericException.Result
Return 0
End Try
End Function
This is the second bit of code that actually uses the GetPasswordTimeToExpire function.
'Get the number of seconds till password expires...
Dim intSecondsTillExpire As Integer = GetPasswordTimeToExpire()
'If error getting password time to expire, print message and exit
If strResult.Length > 0 Then
Listbox_Bad(strResult, True)
Exit Sub
End If
'Calculate the number of days till the password expires
Dim tsTimeTillExpire As TimeSpan = TimeSpan.FromSeconds(intSecondsTillExpire)
Dim intDaysTillExpire As Integer = tsTimeTillExpire.Days
'If there is less than one day till the password expires, set the message to hours
'Otherwise, set it to days
If intDaysTillExpire = 0 Then
'Calculate the number of hours till the password expires
Dim intHoursTillExpire As Integer = tsTimeTillExpire.Hours
'Display message
lblPasswordWarning.Text = "Your password will expire in " & intHoursTillExpire & _
" hours!"
Else
'Display message
lblPasswordWarning.Text = "Your password will expire in " & intDaysTillExpire & _
" days."
End If
'If the password will expire in the next 7 days, make the warning more noticable
If intDaysTillExpire <= 7 Then
lblPasswordWarning.CssClass = "TextBoxBad"
End If
Do I just paste these into a .aspx file and call it done?