AccountManagement.UserPrincipal.Current takes 5 seconds
I'm using the new .NET 3.5 System.DirectoryServices.AccountManagement API.
In my code, I need to determine the current System.DirectoryServices.AccountManagement.UserPrincipal.
My naive approach was to do this:
using AccountManagement = System.DirectoryServices.AccountManagement; ... // Determine current UserPrincipal. // (On my machine, this blocks for 5 seconds) // AccountManagement.UserPrincipal principal = AccountManagement.UserPrincipal.Current;
My computer is a stand-alone machine running Vista. I am not part of any domain, etc.
Any ideas on how to improve the performance?
My guess is that since DirectoryServices is geared towards looking up data in a directory, in this case Active Directory, and you are on a single machine, it times out with the request to find a Domain Controller.
I assume you want to get the name of the current user, and you can do that by using the "old" System.Security.Principal.WindowsIdentity.GetCurrent() method instead.
If you need the principal for that user, you can use this code:
WindowsIdentity ident = WindowsIdentity.GetCurrent(); IPrincipal principal = new WindowsPrincipal(ident);