Technical Article

Date Password Last Set in AD

,

Simply add this CLR function to your database and no more fighting with that long pwdLastSet attribute from Active Directory.

/// <summary> 
/// DatePwdLastSet 
/// Written by Mitch Spruill 
/// </summary> 
/// <param name="adPwdLastSet">The pwdLastSet attribute from Active Directory</param> 
/// <returns>SqlDateTime</returns> 
         
[Microsoft.SqlServer.Server.SqlFunction] 
public static SqlDateTime DatePwdLastSet(string adPwdLastSet) 
{ 
    Int64 i64 = 0; // if blank or null string return null 
    if (string.IsNullOrEmpty(adPwdLastSet) == false) 
    { 
        // attempt to convert to Int64 
        if (Int64.TryParse(adPwdLastSet, out i64)) 
        { 
            // this will catch the smarties that try to pass in a "0" string 
            // but this really means that the password has expired and the  
            // user must change their password on next login. 
            if (i64 > 0) 
            { 
                // Convert to a DateTime and return it. 
                DateTime pwdLastSet = DateTime.FromFileTime(i64); 
                return pwdLastSet; 
            } 
        } 
    } // if it doesn't convert you must revert. 
    return SqlDateTime.Null; 
}

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating