当前位置 : 主页 > 编程语言 > c语言 >

如何通过AD DirectoryEntry在VB.NET中设置AccountExpires

来源:互联网 收集:自由互联 发布时间:2021-06-24
我需要在AD DirectoryEntry中设置accountExpires属性找不到简单的答案. 找到一些信息; http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal.aspx http://social.msdn.microsoft.com/F
我需要在AD DirectoryEntry中设置accountExpires属性找不到简单的答案.
找到一些信息;

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

感谢Brian,看起来上面写的大量代码可以简化;

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

在VB.NET中返回AccountExpires和其他largeInteger问题的函数

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

另一种方法

Convert LDAP AccountExpires to DateTime in C#

这样的事情会导致您的帐户在30天后过期:

Dim de As New DirectoryEntry("LDAP://cn=foo,cn=users,dc=contoso,dc=com")

de.Properties["accountExpires"].Value = DateTime.UtcNow.AddDays(30).ToFileTime()
de.CommitChanges()
网友评论