AccountManagement.UserPrincipal.Current 需要 5 秒

Ade*_*zah 1 .net

我正在使用新的 .NET 3.5 System.DirectoryServices.AccountManagement API

在我的代码中,我需要确定当前的 System.DirectoryServices.AccountManagement.UserPrincipal。

我天真的做法是这样做:

using AccountManagement = System.DirectoryServices.AccountManagement;

...

// Determine current UserPrincipal.
// (On my machine, this blocks for 5 seconds)
//

AccountManagement.UserPrincipal principal = AccountManagement.UserPrincipal.Current;
Run Code Online (Sandbox Code Playgroud)

我的计算机是运行 Vista 的独立计算机。我不属于任何域等。

关于如何提高性能有什么想法吗?

Mik*_*son 5

我的猜测是,由于 DirectoryServices 旨在查找目录(在本例中为 Active Directory)中的数据,并且您位于单台计算机上,因此查找域控制器的请求会超时。

我假设您想获取当前用户的名称,您可以使用“旧”System.Security.Principal.WindowsIdentity.GetCurrent()方法来实现。

如果您需要该用户的主体,可以使用以下代码:

WindowsIdentity ident = WindowsIdentity.GetCurrent();
IPrincipal principal = new WindowsPrincipal(ident);
Run Code Online (Sandbox Code Playgroud)