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.")
Else
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()


Try
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
MessageBox.Show(PasswordExpiration.ToString())
End Sub

Comments

Popular posts from this blog

Desempeño de SQL Server 2008 R2 y Max Worker Threads

Cómo identificar consultas más pesadas en SQL Server

SQL Server La longitud de los datos LOB (2200100) que se van a replicar excede el máximo configurado 65536.