Как установить AccountExpires в VB.NET через AD DirectoryEntry

Мне нужно было установить свойство accountExpires в AD DirectoryEntry, я не нашел простого ответа. Нашел некоторую информацию;

http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal.aspx

http://social.msdn.microsoft.com/Forums/en -US / vbgeneral / thread / 182bfb6a-8b23-4c96-9379-101a4d91241a

http://www.rlmueller.net/AccountExpires.htm

Видел некоторые статьи о ADS ****. Dll, но не видел думаю, мне нужно было использовать этот метод

        Dim valueToSet As Date = Now.AddDays(10)
        Dim ADSPath As String = "LDAP://cn=..."
        Dim de As DirectoryEntry = New DirectoryEntry(ADSPath)
        Dim d As TimeSpan = valueToSet.ToUniversalTime - Date.Parse("01/01/1601")
        Dim ValueToSetAsString As String = d.Ticks.ToString
        ' it appears that the ticks value is too large for the value of the directory entry
        ' converting to a string (18 chars or so) works!
        de.Properties("accountexpires").Value = ValueToSetAsString

Благодаря Брайану, похоже, что большой объем кода, написанного выше, можно упростить;

        de.Properties("accountexpires").Value = valueToSet.ToFileTime.ToString

Функция для возврата AccountExpires и других задач largeInteger в VB.NET

        Function ConvertADValueToDateTime(ByVal li As Object) As DateTime
        ' http://bytes.com/topic/visual-basic-net/answers/512901-lastlogontimestamp

        Try
            Dim lngHigh = li.HighPart
            Dim lngLow = li.LowPart
            Dim lastLogon = (lngHigh * 2 ^ 32) - lngLow
            Dim returnDateTime As DateTime = DateTime.FromFileTime(lastLogon)
            Return returnDateTime
        Catch ex As Exception
            Return Nothing
        End Try

    End Function

Пример использования:

            Dim d As DateTime = ConvertADValueToDateTime(de.Properties("accountexpires").value)               
            If d = "01/01/1601" Then
                ' no expiry date
                Return Nothing
            Else
                Return d
            End If

Альтернативный метод

Конвертировать LDAP AccountExpires в DateTime в C #

5
задан Community 23 May 2017 в 11:59
поделиться