1

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?

Adel Hazzah
  • 8,737
  • 2
  • 20
  • 16

1 Answers1

5

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);
Mikael Svenson
  • 39,181
  • 7
  • 73
  • 79
  • I need to know the current AccountManagement.Principal so I can use it to call other .NET AccountManagement API methods. Unfortunately, the WindowsIdentity-based solution you mentioned represents a completely different object model that can't be used in the context of the the AccountManagement API. – Adel Hazzah Apr 09 '10 at 22:51