【问题标题】:Fastest way of reading a property from LDAP/Active Directory Search results从 LDAP/Active Directory 搜索结果中读取属性的最快方法
【发布时间】:2014-03-27 09:00:13
【问题描述】:
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();
    }
}

从上面的示例中,检索属性“givenname”并将其存储到字符串变量中的最有效方法是什么?

【问题讨论】:

    标签: c# c#-4.0 active-directory ldap


    【解决方案1】:

    由于您在搜索中加载了属性列表中的属性,因此只需在搜索结果中访问该属性:

    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();
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多