PrincipalContext is very slow for searching local users while connected to a network

Viewed 69
private static PrincipalContext _ctx = new PrincipalContext(ContextType.Machine,Environment.MachineName);
Principal.FindByIdentity(_ctx, IdentityType.Sid, "username or sid");


Pretty simple stuff right? However, it's very slow, about 50ms for every call to FindByIdentity(). The interesting part is that when no network is connected, it is super fast. So I guess it has something to do with querying remote users in SAM. But as you can see I have limited the PrincipalContext search to the local machine. I have one workaround lying around in my code and commented out, but I'd prefer to sort out the problem , else use a workaround if it proves to be impossible because of "by design" or a dotnet bug.

To put things in perspective, the method could get called 100000 times and that amounts to 5000,000 ms or 5000 seconds! And this is worst-case scenario.

0 Answers
Related