也可以使用以下簡單應用程式觸發問題:
Stopwatch watch = new Stopwatch();
string[] userNames = {"JunkName", "DummyName"};
foreach (var name in userNames)
{
watch.Reset();
watch.Start();
if (DirectoryEntry.Exists("WinNT://" Environment.MachineName "/" name))
{
Console.WriteLine($"user {name} found in " watch.ElapsedMilliseconds "ms");
}
else
{
Console.WriteLine($"user {name} not found in " watch.ElapsedMilliseconds "ms");
}
}
沒有正確的根本原因可以說明是 Microsoft API 問題還是其他問題。
但是大多數搜索結果都提到如果您搜索一個不存在的用戶,而不是回傳 false,該DirectoryEntry.Exists方法將拋出例外。
但是在我們的示例應用程式中,它沒有拋出任何例外。
此外,我嘗試了以下一些建議來檢查這些示例代碼需要多少執行時間,發現這些第一次也需要超過 11 秒。
示例 1:
using (PrincipalContext pc = new PrincipalContext(ContextType.Machine))
{
UserPrincipal up = UserPrincipal.FindByIdentity(pc,
IdentityType.SamAccountName, name);
UserExists = (up != null);
}
示例 2:
DirectoryEntry dirEntryLocalMachine =
new DirectoryEntry("WinNT://" Environment.MachineName ",computer");
bool UserExists =
dirEntryLocalMachine.Children.Find(userIdentity, "user") != null;
這只是對作業系統更新會導致問題的高度懷疑,可能是我的假設是錯誤的。
提前致謝
uj5u.com熱心網友回復:
有一天我遇到了同樣的延遲,所以我寫了這個更快的函式,也許它可以幫助你:
private string FindUserSID(string domain, string user)
{
string output;
try
{
NTAccount account = new NTAccount(domain @"\" user);
SecurityIdentifier s = (SecurityIdentifier)account.Translate(typeof(SecurityIdentifier));
output = s.ToString();
}
catch (IdentityNotMappedException)
{
output = null;
}
return output;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/365819.html
下一篇:檔案移動失敗后消失
