首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从ActiveDirectory读取属性‘Mcs AdmPwdExpirationTime`’

如何从ActiveDirectory读取属性‘Mcs AdmPwdExpirationTime`’
EN

Stack Overflow用户
提问于 2017-10-13 06:38:27
回答 2查看 2.8K关注 0票数 0

我正在尝试从读取管理员密码的终止时间

代码语言:javascript
复制
Dim DC = New  PrincipalContext(ContextType.Domain)
Dim cmp = ComputerPrincipal.FindByIdentity(DC, hostnm)
Dim desting As String = cmp.DistinguishedName
Dim de As New DirectoryEntry("LDAP://" & desting)
pwdexp = de.Properties("ms-Mcs-AdmPwdExpirationTime").Value.ToString()

但我看到的只是<COM Type>

但是,PowerShell很容易读取管理员密码的过期时间。

代码语言:javascript
复制
$TestValue = [adsi]"LDAP://CN=xxx,OU=xxx,OU=xxx,OU=xxx,OU=xxx,DC=xxx,DC=xxx,DC=xx"
$TestValue.ConvertLargeIntegerToInt64($Testvalue."ms-Mcs-AdmPwdExpirationTime"[0])

我知道有这样一种财产:

有趣的是,但是我可以读到另一个参数ms-Mcs-AdmPwd

代码语言:javascript
复制
Dim DC = New  PrincipalContext(ContextType.Domain)
Dim cmp = ComputerPrincipal.FindByIdentity(DC, hostnm)
Dim desting As String = cmp.DistinguishedName
Dim de As New DirectoryEntry("LDAP://" & desting)
pwdexp = de.Properties("ms-Mcs-AdmPwdExpirationTime").Value.ToString()

从调试器中可以看出值:

如何正确读取属性ms-Mcs-AdmPwdExpirationTime

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-15 07:52:05

返回值是DateTime,它在AD中表示为LargeInteger。你必须把它转换成能够读懂它。

注意,在PowerShell中,您正在使用ConvertLargeIntegerToInt64转换值。所以,我们也需要先做同样的事。

C#中的代码

代码语言:javascript
复制
            /// <summary>
            /// Decodes IADsLargeInteger objects into a FileTime format (long)
            /// </summary>
            public static long ConvertLargeIntegerToLong(object largeInteger)
            {
                var type = largeInteger.GetType();
                var highPart = (int)type.InvokeMember("HighPart", BindingFlags.GetProperty, null, largeInteger, null);
                var lowPart = (int)type.InvokeMember("LowPart", BindingFlags.GetProperty, null, largeInteger, null);

                return (long)highPart << 32 | (uint)lowPart;
            }

在VB.NET中(使用http://converter.telerik.com/)

代码语言:javascript
复制
''' <summary>
''' Decodes IADsLargeInteger objects into a FileTime format (long)
''' </summary>
Public Shared Function ConvertLargeIntegerToLong(largeInteger As Object) As Long
    Dim type = largeInteger.[GetType]()
    Dim highPart = CInt(type.InvokeMember("HighPart", BindingFlags.GetProperty, Nothing, largeInteger, Nothing))
    Dim lowPart = CInt(type.InvokeMember("LowPart", BindingFlags.GetProperty, Nothing, largeInteger, Nothing))

    Return CLng(highPart) << 32 Or CUInt(lowPart)
End Function

然后使用以下方法读取日期值

代码语言:javascript
复制
var pwdExpTime = DateTime.FromFileTime(ConvertLargeIntegerToLong(de.Properties["ms-Mcs-AdmPwdExpirationTime"].Value));

在VB.NET中

代码语言:javascript
复制
Dim pwdExpTime = DateTime.FromFileTime(ConvertLargeIntegerToLong(de.Properties("ms-Mcs-AdmPwdExpirationTime").Value))
票数 2
EN

Stack Overflow用户

发布于 2022-06-21 10:24:27

使用接受的答案并将ms-Mcs-AdmPwdExpirationTime设置为133004656837955708 (应该会导致23.06.2022 15:48:03),我得到了一个错误。

使用此日期,lowPart为负值,不能使用CUInt进行强制转换。

所以我混合了两种解决方案:

代码语言:javascript
复制
Public Shared Function ConvertLargeIntegerToLong(largeInteger As Object) As Long
    Dim type = largeInteger.[GetType]()
    Dim highPart = CInt(type.InvokeMember("HighPart", BindingFlags.GetProperty, Nothing, largeInteger, Nothing))
    Dim lowPart = CInt(type.InvokeMember("LowPart", BindingFlags.GetProperty, Nothing, largeInteger, Nothing))
    If lowPart < 0 Then
        Dim tempStr as String = "&H" + CStr(Hex(highPart)) + CStr(Hex(lowPart))
        Return Val(tempStr)
    Else
        Return CLng(highPart) << 32 Or CUInt(lowPart)
    End If
End Function
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46723874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档