我想将所有用户属性从活动目录分配到List<用户>使用LINQ的属性。
为此,我尝试了一些代码,但我无法在Manager
用户管理器属性上分配List
属性。
因为,从Active目录中分配Manager属性值,我们需要在Active Directory上搜索manager用户,并根据搜索结果将manager属性值分配给List of User.Manager属性。
这是我的代码,
public List< User > GetADUsers()
{
DirectorySearcher searcher = new DirectorySearcher(ADEntry);
searcher.Filter = "(&(objectClass=user)objectCategory=person)";
SearchResultCollection resultCol = searcher.FindAll();
return
(from serachresult in resultCol.OfType<SearchResult>()
let result = serachresult.GetDirectoryEntry()
result.Properties["sAMAccountName"].Value != null
select new User
{
GivenName = result.Properties.Contains("givenname") ?
Convert.ToString(result.Properties["givenname"][0]) : "",
SurName = result.Properties.Contains("sn") ?
Convert.ToString(result.Properties["sn"][0]) : "",
Location = result.Properties.Contains("physicalDeliveryOfficeName") ?
Convert.ToString(result.Properties["physicalDeliveryOfficeName"][0]) : "",
Manager= ?// Here I need to assign the manager attribute from active directory
}
).ToList();
}
通过使用下面提到的代码,我使用Active Directory
对象从directory entry
获取manager属性。我想将此代码放在上面提到的LINQ查询上
在Manager属性上。我们如何在LINQ Select子句中实现这一点。
DirectoryEntry DomainRoot = AD.GetDirectoryEntry();
using (DirectorySearcher Search = new DirectorySearcher())
{
Search.SearchRoot = DomainRoot;
Search.Filter = "(&(distinguishedName=" +
Convert.ToString(result.Properties["manager"][0]) + "))";
SearchResult Result = Search.FindOne();
if (Result != null)
{
DirectoryEntry Manager = Result.GetDirectoryEntry();
//This Manager.Name value I have assign above mentioned LINQ
user.Manager = Manager.Name.Substring(3);
Manager.Close();
}
}
请帮我在LINQ查询管理器属性位置分配管理器名称!感谢。
答案 0 :(得分:1)
尝试将您的经理评估转移到一个新功能,该功能采用'结果'并返回经理名称。 然后从您的链接语句中调用它:Manager = GetManagerName(result)
public string GetManagerName(DirectoryEntry dirEntry)
{
DirectoryEntry DomainRoot = AD.GetDirectoryEntry();
using (DirectorySearcher search = new DirectorySearcher())
{
search.SearchRoot = DomainRoot;
search.Filter = "(&(distinguishedName=" + Convert.ToString(dirEntry.Properties["manager"][0]) + "))";
SearchResult result = search.FindOne();
if (result != null)
{
using (DirectoryEntry mgr = result.GetDirectoryEntry())
{
return mgr.Name.Substring(3);
}
}
return string.Empty;
}
}