How to convert Active Directory pwdLastSet to Date/Time

后端 未结 2 958
走了就别回头了
走了就别回头了 2020-12-06 18:08
    public static string GetProperty(SearchResult searchResult, string PropertyName)
    {
        if (searchResult.Properties.Contains(PropertyName))
        {
             


        
相关标签:
2条回答
  • 2020-12-06 18:34

    According to the MSDN documentation:

    This value is stored as a large integer that represents the number of 100 nanosecond intervals since January 1, 1601 (UTC).

    This aligns perfectly with DateTime.FromFileTimeUtc, as described here.

    And I'm not sure why you feel the need to do the low level manipulation of the integer. I think you could just cast it.

    So just do:

    long value = (long)objResult.Properties["pwdLastSet"][0];
    DateTime pwdLastSet = DateTime.FromFileTimeUtc(value);
    
    0 讨论(0)
  • 2020-12-06 18:49

    You can get the last password set date of a directory user in human readable form as easy as pie. To achieve this you can use nullable LastPasswordSet property of UserPrincipal class from System.DirectoryServices.AccountManagement namespace.

    If User must change password at next logon option is checked then LastPasswordSet property returns null value. Otherwise it returns the last date and time the password was set in type DateTime.

    using(PrincipalContext context = new PrincipalContext(ContextType.Domain))
    {
        UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, Username);
        //? - to mark DateTime type as nullable
        DateTime? pwdLastSet = (DateTime?)user.LastPasswordSet;
        ...
    }
    

    MSDN: UserPrincipal
    MSDN: LastPasswordSet

    0 讨论(0)
提交回复
热议问题