Search code examples
c#console-applicationimpersonation

Console App cannot read full directory listing for access based enumberation as domain admin


I have a console application that is either impersonating the Domain Admin or runs as the domain admin. It is supposed to read all of the directory names in a folder with Access based enumeration enabled and then apply some permissions on the folders. The part of the application getting the directories is:

        string dirPath = ConfigurationManager.AppSettings["EPath"] + @"2019 Working Files";

        DirectoryInfo di = new DirectoryInfo(dirPath);
        DirectoryInfo[] directories = di.GetDirectories();

        foreach (DirectoryInfo dir in directories)
        {
            // Doing something...

        }

I have logged into the server where this needs to run as both the local and domain admins and run this. I have also tried impersonating the same users and all that is retrieved is 45 of over 6500 directory information items. The code I use for impersonating the administrator is:

        string impersonatedUserName = ConfigurationManager.AppSettings["EstimatingPathUser"].ToString();
        string impersonationDomain = ConfigurationManager.AppSettings["EstimatingPathDomain"].ToString();
        string impersonatedUserPassword = ConfigurationManager.AppSettings["EstimatingPathPassword"].ToString();
        SafeTokenHandle safeTokenHandle;

        string dirPath = ConfigurationManager.AppSettings["EPath"] + @"2019 Working Files";

        DirectoryInfo di = new DirectoryInfo(dirPath);
        DirectoryInfo[] directories = di.GetDirectories();

        if (LogonUser(impersonatedUserName, impersonationDomain, impersonatedUserPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out safeTokenHandle))
        {
            directories = di.GetDirectories();

        }

        foreach (DirectoryInfo dir in directories)
        {
            // Doing something...
        }

I have even tried impersonating the Owner of all of the folders. Nothing seems to work.


Solution

  • Overnight the server was rebooted and the application runs properly now. All 6519 folders were found and processed.