Creating a PrincipalSearcher takes very long

120 Views Asked by At

Creating an instance of a PrincipalSearcher for accessing a local ActiveDirectory takes about 11 - 22 seconds. Interestingly the time is always 11 or 22 seconds, +/- a few milliseconds. The OU I'm trying to read out consists of not more than 30 users and 20 groups. If I understand it right the AD is completely read during the creation of the PrincipalSearcher. But because my AD is tiny I would not expect it to take that long. Am I doing something wrong or is this an expected behavior?


This is the test code I use to reproduce the described behavior.

const string name = "localhost:389";
const string container = "OU=OUNAME,DC=DCNAME,DC=local";
var principalContext = new PrincipalContext(ContextType.ApplicationDirectory, name, container);
var userPrincipal = new UserPrincipal(principalContext);
// The next call takes about 22 - 44 seconds
var principalSearcher = new PrincipalSearcher(userPrincipal);
var result = principalSearcher.FindAll();

When I do a performance profiling I get the following result: Performance Profiling Results


UPDATE #1:

The local AD is done with AD LDS and I found this question that seems to describe a similar problem. But I have both, the AD and the application, running on the same machine and I'm using localhost or 127.0.0.1. So this did not helped in my case.


UPDATE #2:

If I'm no longer connected to the company's domain, the same code takes less that 100ms. What could cause that different behavior? And more important, how can I overcome that behavior while connected to the domain?

1

There are 1 best solutions below

1
On

Try using the options parameter of the PrincipalContext constructor and see if some combination solves the problem for you.

The default (if you don't specify anything) is:

ContextOptions.Negotiate | ContextOptions.Signing | ContextOptions.Sealing

But see what combination of ContextOptions makes a difference, if any.