web analytics

How to calculate the Active Directory's account expired date in C#?

Options

codeling 1599 - 6654
@2015-12-17 14:45:12

In the Active Directory, the accountExpire attribute is used to specify the date and time when the account expires. This value represents the number of 100 nanosecond intervals since January 1, 1601 (UTC).

The following C# code demostrates you how to convert this value to Datetime object in .NET Framework:

using System.DirectoryServices;

DirectoryEntry entry;


DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com",  "username", "password");

DirectorySearcher searcher = new DirectorySearcher(directoryEntry)

{

   PageSize = int.MaxValue,

   Filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=AnAccountName))"

};

 var result = searcher.FindOne();

if (result != null)

{

     if (result.Properties.Contains("AccountExpires"))

    {

          DateTime expiredDateTime =  DateTime.FromFileTime(
Convert.ToInt64(result.
Properties.Contains("AccountExpires"))[0].ToString()));

    }

}

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com