問題描述
我正在嘗試通過 Active Directory 遞歸地獲取用戶的所有直接報告.所以給定一個用戶,我最終會得到一個所有用戶的列表,這些用戶有這個人作為經(jīng)理,或者有一個人作為經(jīng)理,有一個人作為經(jīng)理......最終將輸入用戶作為經(jīng)理.
I'm trying to get all the direct reports of a User through Active Directory, recursively. So given a user, i will end up with a list of all users who have this person as manager or who have a person as manager who has a person as manager ... who eventually has the input user as manager.
我目前的嘗試相當(dāng)緩慢:
My current attempt is rather slow:
private static Collection<string> GetDirectReportsInternal(string userDN, out long elapsedTime)
{
Collection<string> result = new Collection<string>();
Collection<string> reports = new Collection<string>();
Stopwatch sw = new Stopwatch();
sw.Start();
long allSubElapsed = 0;
string principalname = string.Empty;
using (DirectoryEntry directoryEntry = new DirectoryEntry(string.Format("LDAP://{0}",userDN)))
{
using (DirectorySearcher ds = new DirectorySearcher(directoryEntry))
{
ds.SearchScope = SearchScope.Subtree;
ds.PropertiesToLoad.Clear();
ds.PropertiesToLoad.Add("directReports");
ds.PropertiesToLoad.Add("userPrincipalName");
ds.PageSize = 10;
ds.ServerPageTimeLimit = TimeSpan.FromSeconds(2);
SearchResult sr = ds.FindOne();
if (sr != null)
{
principalname = (string)sr.Properties["userPrincipalName"][0];
foreach (string s in sr.Properties["directReports"])
{
reports.Add(s);
}
}
}
}
if (!string.IsNullOrEmpty(principalname))
{
result.Add(principalname);
}
foreach (string s in reports)
{
long subElapsed = 0;
Collection<string> subResult = GetDirectReportsInternal(s, out subElapsed);
allSubElapsed += subElapsed;
foreach (string s2 in subResult)
{
result.Add(s2);
}
}
sw.Stop();
elapsedTime = sw.ElapsedMilliseconds + allSubElapsed;
return result;
}
本質(zhì)上,這個函數(shù)將一個可分辨的名稱作為輸入(CN=Michael Stum,OU=test,DC=sub,DC=domain,DC=com),因此,對 ds.FindOne() 的調(diào)用很慢.
Essentially, this function takes a distinguished Name as input (CN=Michael Stum, OU=test, DC=sub, DC=domain, DC=com), and with that, the call to ds.FindOne() is slow.
我發(fā)現(xiàn)搜索 userPrincipalName 的速度要快得多.我的問題:sr.Properties["directReports"] 只是一個字符串列表,也就是distinguishedName,搜索起來似乎很慢.
I found that it is a lot faster to search for the userPrincipalName. My Problem: sr.Properties["directReports"] is just a list of strings, and that is the distinguishedName, which seems slow to search for.
我想知道,有沒有一種快速的方法可以在 distinctName 和 userPrincipalName 之間進(jìn)行轉(zhuǎn)換?或者,如果我只有可使用的專有名稱,是否有更快的方法來搜索用戶?
I wonder, is there a fast way to convert between distinguishedName and userPrincipalName? Or is there a faster way to search for a user if I only have the distinguishedName to work with?
感謝您的回答!搜索經(jīng)理字段將功能從 90 秒改進(jìn)為 4 秒.這是新的和改進(jìn)的代碼,它更快、更易讀(請注意,elapsedTime 功能中很可能存在錯誤,但該函數(shù)的實際核心是有效的):
Thanks to the answer! Searching the Manager-Field improved the function from 90 Seconds to 4 Seconds. Here is the new and improved code, which is faster and more readable (note that there is most likely a bug in the elapsedTime functionality, but the actual core of the function works):
private static Collection<string> GetDirectReportsInternal(string ldapBase, string userDN, out long elapsedTime)
{
Collection<string> result = new Collection<string>();
Stopwatch sw = new Stopwatch();
sw.Start();
string principalname = string.Empty;
using (DirectoryEntry directoryEntry = new DirectoryEntry(ldapBase))
{
using (DirectorySearcher ds = new DirectorySearcher(directoryEntry))
{
ds.SearchScope = SearchScope.Subtree;
ds.PropertiesToLoad.Clear();
ds.PropertiesToLoad.Add("userPrincipalName");
ds.PropertiesToLoad.Add("distinguishedName");
ds.PageSize = 10;
ds.ServerPageTimeLimit = TimeSpan.FromSeconds(2);
ds.Filter = string.Format("(&(objectCategory=user)(manager={0}))",userDN);
using (SearchResultCollection src = ds.FindAll())
{
Collection<string> tmp = null;
long subElapsed = 0;
foreach (SearchResult sr in src)
{
result.Add((string)sr.Properties["userPrincipalName"][0]);
tmp = GetDirectReportsInternal(ldapBase, (string)sr.Properties["distinguishedName"][0], out subElapsed);
foreach (string s in tmp)
{
result.Add(s);
}
}
}
}
}
sw.Stop();
elapsedTime = sw.ElapsedMilliseconds;
return result;
}
推薦答案
首先,當(dāng)您已經(jīng)擁有要查找的 DN 時,不需要將 Scope 設(shè)置為subtree".
First off, setting Scope to "subtree" is unnecessary when you already have the DN you are looking for.
此外,如何查找manager"屬性是您要查找的人的所有對象,然后迭代它們.這通常應(yīng)該比其他方式更快.?/p>
Also, how about finding all objects whose "manager" property is the person you look for, then iterating them. This should generally be faster than the other way around.
(&(objectCategory=user)(manager=<user-dn-here>))
以下內(nèi)容很重要,但到目前為止僅在對此答案的評論中提及:
當(dāng)過濾器字符串按上述方式構(gòu)建時,存在用對 DN 有效但在過濾器中具有特殊含義的字符破壞它的風(fēng)險.這些必須轉(zhuǎn)義:
When the filter string is built as indicated above, there is the risk of breaking it with characters that are valid for a DN, but have special meaning in a filter. These must be escaped:
* as 2a
( as 28
) as 29
as 5c
NUL as
主站蜘蛛池模板:
国产在线永久免费
|
av在线免费网站
|
国产一区二区三区在线免费观看
|
精品国产31久久久久久
|
精品一区国产
|
国产精品视频播放
|
国产精品不卡
|
久久久精品影院
|
在线视频一区二区三区
|
国产91精品久久久久久久网曝门
|
欧美日韩久久精品
|
欧美性成人
|
日韩精品免费
|
国产高清一区二区
|
亚洲综合小视频
|
91免费电影|
午夜精品久久久久久久99黑人
|
亚洲三级视频
|
精品免费国产视频
|
亚洲一区视频在线播放
|
区一区二在线观看
|
99re在线视频观看
|
日韩在线免费视频
|
天堂视频中文在线
|
免费看日韩视频
|
福利电影在线
|
成人影院一区二区三区
|
国产在线a
|
色就干|
中文字幕久久精品
|
超碰97免费在线
|
久久久久国产精品午夜一区
|
欧美一级欧美三级在线观看
|
国产一区不卡
|
涩爱av一区二区三区
|
亚洲永久入口
|
逼逼视频
|
玖玖国产|
婷婷丁香在线视频
|
亚洲精品欧美精品
|
久久精品国产99国产
|