Tuesday, September 04, 2007

Fecha de expiracion desde VB.NET

Imports System.DirectoryServices
Imports System.Reflection

Public Shared Function PasswordExpiration() As DateTime

Dim user As DirectoryEntry
Dim root As DirectoryEntry
Dim rootDSE As DirectoryEntry

Dim defaultContext As String

Dim maxPwdVal As Object
Dim pwdLastSetVal As Object

Dim pwdLastSet As Int64
Dim pwdLastSetDate As DateTime
Dim pwdExpirationDate As DateTime
Dim maxPwd As TimeSpan

rootDSE = New DirectoryEntry("LDAP://RootDSE")

defaultContext = DirectCast(rootDSE.Properties("defaultNamingContext").Value, System.String)

'change this to point to the user you wish to check
user = New DirectoryEntry("LDAP://cn=pruebas,cn=Users," + defaultContext)
root = New DirectoryEntry("LDAP://" + defaultContext)

pwdLastSetVal = user.Properties("pwdLastSet").Value
maxPwdVal = root.Properties("maxPwdAge").Value

pwdLastSet = GetInt64FromLargeInteger(pwdLastSetVal)

If pwdLastSet <= 0L Then
Throw New InvalidOperationException("User's password either doesn() 't expire or has already expired.")
pwdLastSetDate = DateTime.FromFileTime(pwdLastSet)
maxPwd = New TimeSpan(GetInt64FromLargeInteger(maxPwdVal))

'these seem to be stored as negative numbers in AD, so I use Subtract instead of Add here...
pwdExpirationDate = pwdLastSetDate.Subtract(maxPwd)
End If

Return pwdExpirationDate

End Function

'Imports System.Reflection is implied here...

Public Shared Function GetInt64FromLargeInteger(ByVal largeInteger As Object) As Long
Dim lowPart As Integer
Dim lBytes() As Byte
Dim highPart As Integer
Dim hBytes() As Byte
Dim valBytes(7) As Byte

Dim longVal As Long
Dim largeIntType As Type

largeIntType = largeInteger.GetType()

highPart = CType(largeIntType.InvokeMember("HighPart", BindingFlags.GetProperty Or BindingFlags.Public, Nothing, largeInteger, Nothing), Integer)
lowPart = CType(largeIntType.InvokeMember("LowPart", BindingFlags.GetProperty Or BindingFlags.Public, Nothing, largeInteger, Nothing), Integer)

lBytes = BitConverter.GetBytes(lowPart)
hBytes = BitConverter.GetBytes(highPart)

lBytes.CopyTo(valBytes, 0)
hBytes.CopyTo(valBytes, 4)

longVal = BitConverter.ToInt64(valBytes, 0)

Return longVal

Catch e As MissingMethodException
Throw New ArgumentException("Invalid COM object passed as parameter. Object must be IADsLargeInteger.", e)
End Try
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub

No comments:

SQL Server 2016 Stretch Databases

El SQL Server Stretch Database permite que los datos históricos o de bitácoras puedan ser movidos hacia Azure, específicamente permite mover...