[英]Fastest way of reading a property from LDAP/Active Directory Search results
using (DirectoryEntry rootEntry = new DirectoryEntry(ConfigurationKeys.Ldap, string.Empty, string.Empty, AuthenticationTypes.None))
{
using (DirectorySearcher adSearch = new DirectorySearcher(rootEntry))
{
adSearch.SearchScope = SearchScope.Subtree;
adSearch.PropertiesToLoad.Add("givenname");
adSearch.PropertiesToLoad.Add("mail");
adSearch.Filter = "(mail=myemail@mydomain.org)";
SearchResult adSearchResult = adSearch.FindOne();
}
}
From the sample above, what is the most effecient way of retrieving the property "givenname" and storing it into a string variable? 从上面的示例中,检索属性“givenname”并将其存储到字符串变量中的最有效方法是什么?
Since you have the property in the list of properties to load in the search, just access that property in the search result: 由于您要在搜索中加载的属性列表中包含该属性,因此只需在搜索结果中访问该属性:
using (DirectoryEntry rootEntry = new DirectoryEntry(ConfigurationKeys.Ldap, string.Empty, string.Empty, AuthenticationTypes.None))
{
using (DirectorySearcher adSearch = new DirectorySearcher(rootEntry))
{
adSearch.SearchScope = SearchScope.Subtree;
adSearch.PropertiesToLoad.Add("givenname");
adSearch.PropertiesToLoad.Add("mail");
adSearch.Filter = "(mail=myemail@mydomain.org)";
SearchResult adSearchResult = adSearch.FindOne();
// make sure the adSearchResult is not null
// and the "givenName" property is not null (could be empty / null)
if(adSearchResult != null && adSearchResult.Properties["givenName"] != null)
{
// make sure the givenName property contains at least one string value
if (adSearchResult.Properties["givenName"].Count > 0)
{
string givenName = adSearchResult.Properties["givenName"][0].ToString();
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.