Here's a nice sample of how to get access to things like if an Account has been disabled or to get the Password Expiration Date. You will need the Interop.ActiveDs.dll for this code to work.
using System;
using System.Security.Principal;
using System.Diagnostics;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
foreach (IdentityReference group in WindowsIdentity.GetCurrent().Groups)
{
DirectoryEntry entry = new DirectoryEntry(FriendlyDomainToLdapDomain("<FQDN>"));
ActiveDs.IADsUser native = (ActiveDs.IADsUser)CurrentUser.NativeObject;
Debug.Print("AccountDisabled = {0}", native.AccountDisabled);
Debug.Print("AccountExpirationDate = {0}", native.AccountExpirationDate);
Debug.Print("ADsPath = {0}", native.ADsPath);
Debug.Print("BadLoginCount = {0}", native.BadLoginCount);
Debug.Print("GraceLoginsAllowed = {0}", native.GraceLoginsAllowed);
Debug.Print("PasswordExpirationDate = {0}", native.PasswordExpirationDate);
Debug.Print("PasswordLastChanged = {0}", native.PasswordLastChanged);
}
}
private static string FriendlyDomainToLdapDomain(string friendlyDomainName)
{
string ldapPath = null;
try
{
DirectoryContext objContext = new DirectoryContext(DirectoryContextType.Domain, friendlyDomainName);
Domain objDomain = Domain.GetDomain(objContext);
ldapPath = objDomain.Name;
}
catch (DirectoryServicesCOMException e)
{
Debug.Assert(false, e.Message);
Debug.Print("Error: {0}", e.Message);
}
return ldapPath;
}
public static DirectoryEntry CurrentUser
{
get
{
string[] currentUserName = WindowsIdentity.GetCurrent().Name.Split('\\');
string domainDistinguishedName = GetDistinguishedName(currentUserName[0]);
DirectoryEntry domain = new DirectoryEntry("LDAP://" + domainDistinguishedName);
DirectorySearcher searcher = new DirectorySearcher(domain,
String.Format("(sAMAccountName={0})", currentUserName[1]));
SearchResult result = searcher.FindOne();
DirectoryEntry currentUser = null;
if (result != null)
{
currentUser = result.GetDirectoryEntry();
}
return currentUser;
}
}
public static string GetDistinguishedName(string netbiosName)
{
if (string.IsNullOrEmpty(netbiosName))
{
throw new ArgumentNullException("netbiosName");
}
if (!Regex.IsMatch(netbiosName, @"^[-\w]{1,15}$"))
{
throw new ArgumentException("Invalid NetBIOS domain name format. Domain name should be a maximum of 15 alphanumeric characters (including dashes).", "netbiosName");
}
DirectoryEntry globalCatalog = new DirectoryEntry("LDAP://RootDSE");
string configurationPath = (string)globalCatalog.Properties["configurationNamingContext"].Value;
DirectoryEntry partitions = new DirectoryEntry("LDAP://CN=Partitions," + configurationPath);
DirectorySearcher searcher = new DirectorySearcher(partitions,
String.Format("(&(objectClass=crossRef)(nETBIOSName={0}))", netbiosName),
new string[] { "nCName" },
SearchScope.OneLevel);
SearchResult result = searcher.FindOne();
string distinguishedName = null;
if (result != null)
{
distinguishedName = result.Properties["nCName"][0] as string;
}
return distinguishedName;
}
}
}