Search code examples
c#active-directorydirectoryservicesdirectoryentry

Active Directory Is User Deactivated Code Snippet Needed?


Can some post the way to know if a particular user is a deactivated user in the windows ad ?


Solution

  • If you're on .NET 3.5 or can upgrade to .NET 3.5 - have a look at the new System.DirectoryServices.AccountManagement namespace which makes lots of these operations a breeze. See Managing Directory Security Principals in the .NET Framework 3.5 for an intro.

    In your case, you could write your code something like this:

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN")
    
    UserPrincipal user = UserPrincipal.FindByIdentity("somename");
    
    bool locked = user.IsAccountLockedOut();
    

    That's all there is! Most of those everyday operations on users and groups have been vastly improved with .NET 3.5 - use those new capabilities!