I am working an an webapp and in my controller it iterates through all of the domain controllers in a test environment, looking for the most recent lastLogon of all users doing a search from Top Level Domain. It works perfectly when all domain controllers are up and running, however, in this one production environment, with 9 domain controllers, 1 of the domain controllers is having issues and no one can authenticate to it, my app seems to hang, but does not return any errors. I decided to use this below, from Dalton, in https://stackoverflow.com/a/19585559/2698193
public static DateTime findlastlogon(int domainID, string userName)
{
DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, DomainManagement.domainName(domainID), DomainManagement.domainServiceAccountLoginName(domainID), DomainManagement.domainServiceAccountPassword(domainID));
DateTime latestLogon = DateTime.MinValue;
DomainControllerCollection dcc = DomainController.FindAll(context);
Parallel.ForEach(dcc.Cast<object>(), dc1 =>
{
DirectorySearcher ds;
DomainController dc = (DomainController)dc1;
using (ds = dc.GetDirectorySearcher())
{
try
{
ds.Filter = String.Format(
"(sAMAccountName={0})",
userName
);
ds.PropertiesToLoad.Add("lastLogon");
ds.SizeLimit = 1;
SearchResult sr = ds.FindOne();
if (sr != null)
{
DateTime lastLogon = DateTime.MinValue;
if (sr.Properties.Contains("lastLogon"))
{
lastLogon = DateTime.FromFileTime(
(long)sr.Properties["lastLogon"][0]
);
}
if (DateTime.Compare(lastLogon, latestLogon) > 0)
{
latestLogon = lastLogon;
}
}
}
catch (Exception ex)
{
}
}
ds.Dispose();
});
return latestLogon;
}
Is there any way possible to do a check on all domain controllers and skip the ones not responding so it will continue to run without even seeing the downed domain controller(s)? I am unsure of how to do this.
Thanks