tags:

views:

134

answers:

2

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?

+1  A: 

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
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
A: 

I've encountered the same issue while trying to get the current user's full-name. In my case it took ~15 seconds for offline (disconnected) stations. I tried the GetUserFullNameEx Win32API and then encountered other issue (see this post Getting full user name)

Any help with this issue will be more than welcome

Nir