Far*_*d J 7 c# ldap active-directory
我已经解决了从Active Directory中读取一些内容的问题.但结果有一些"ResultPropertyCollection"作为属性.任何机构都知道如何将其转换为列表(如通用列表)?
DirectoryEntry de = new DirectoryEntry("LDAP://" + this.rootLDAP);
DirectorySearcher ds = new DirectorySearcher(de, "(& (objectcategory=Group))");
ds.PropertiesToLoad.Add("samaccountname");
ds.PropertiesToLoad.Add("memberof");
ds.PropertiesToLoad.Add("samaccounttype");
ds.PropertiesToLoad.Add("grouptype");
ds.PropertiesToLoad.Add("member");
ds.PropertiesToLoad.Add("objectcategory");
var r = ( from SearchResult sr in ds.FindAll() select sr ) .ToArray();
Run Code Online (Sandbox Code Playgroud)
谢谢Farzad
mar*_*c_s 10
你不能 - 至少不是很容易.
如果您只想拥有一个SearchResult类型列表,可以使用:
var r = ds.FindAll();
List<SearchResult> results = new List<SearchResult>();
foreach (SearchResult sr in r)
{
results.Add(sr);
}
Run Code Online (Sandbox Code Playgroud)
但是,如果您想要搜索结果中的实际值,则需要做更多工作.
基本上,该集合包含您为搜索定义的所有属性 - 至少只要它们包含值!
所以,你需要做的是创建一个类来保存这些值.这两个元素是memberOf和member本身可以包含多个值(他们是"多值",在广告属性) -所以你需要为这些字符串列表:
public class YourType
{
public string SamAccountName { get; set; }
public int SamAccountType { get; set; }
public int GroupType { get; set; }
public string ObjectCategory { get; set; }
public List<string> MemberOf { get; set; }
public List<string> Member { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后,一旦获得搜索结果,就需要迭代结果并YourType为每个搜索结果创建新实例,并将其粘贴到List<YourType>:
foreach(SearchResult sr in ds.FindAll())
{
YourType newRecord = ConvertToYourType(sr);
}
Run Code Online (Sandbox Code Playgroud)
在该方法中,您需要检查.Properties每个值的集合并提取它:
public YourType ConvertToYourType(SearchResult result)
{
YourType returnValue = new YourType();
returnValue.MemberOf = new List<string>();
returnValue.Member = new List<string>();
if(result.Properties["samAccountName"] != null && result.Properties["samAccountName"].Count > 0)
{
returnValue.SamAccountName = result.Properties["samAccountName"][0].ToString();
}
// ..... and so on for each of your values you need to extracxt
return returnValue;
}
Run Code Online (Sandbox Code Playgroud)