Raskolnikov
Raskolnikov

Reputation: 4009

Read deleted users from Active directory

Is it possible to read deleted users from Active directory?

Simple and short question. I want read this info with C# UserPrincipal, but I am not sure if this is possible.

Upvotes: 1

Views: 1947

Answers (1)

smr5
smr5

Reputation: 2793

Here's how you can search for deleted users using DirectoryEntry and DirectorySearcher. If it's really trivial for you to get the underlying object as UserPrincipal you can cast user object as UserPrincipal.

public static void searchDeletedUsers()
{
   using (DirectoryEntry entry = new DirectoryEntry("LDAP://yourldappath.com"))
   {
      using (DirectorySearcher searcher = new DirectorySearcher(entry))
      {
         searcher.Filter = "(&(isDeleted=TRUE)(objectclass=user))";
         searcher.Tombstone = true;
         var users = searcher.FindAll();
         foreach(var user in users)
         {
            //user will contain the deleted user object
         }
      }
   }
}

Upvotes: 3

Related Questions