【问题标题】:LDAP SearchResult does not contain user propertiesLDAP SearchResult 不包含用户属性
【发布时间】:2013-08-10 10:11:59
【问题描述】:

我正在使用DirectorySearcher.FindOne() 方法。

我在我的 Active Directory 用户属性中指定了Mobile 号码。我的搜索过滤器看起来像这样

(&(ObjectClass=User)(mobile=+11111111111))

使用此过滤器,我可以获得合适的用户。

我还在我的 AD 用户属性中指定了传真号码,但 SearchResult 不包含传真属性。事实上SearchResult 只包含一个属性,但我希望返回所有用户属性,包括传真号码。

我应该修改我的查询以返回传真号码吗?也许需要更改我的 AD 用户或 LDAP 服务器?

【问题讨论】:

    标签: c# active-directory ldap directoryservices


    【解决方案1】:

    使用DirectorySearcher 时,您可以使用PropertiesToLoad 集合来定义将包含在SearchResult 中的属性。如果不指定任何内容,则只会获得可分辨的 LDAP 名称

    所以试试这样的:

    DirectoryEntry root = new DirectoryEntry("LDAP://-your-base-LDAP-path-here-");
    
    DirectorySearcher searcher = new DirectorySearcher(root);
    searcher.Filter = "(&(ObjectClass=User)(mobile=+11111111111))";
    
    // DEFINE what properties you need !
    searcher.PropertiesToLoad.Add("Mobile");
    searcher.PropertiesToLoad.Add("Fax");
    
    SearchResult result = searcher.FindOne();
    
    if (result != null)
    {
       if (result.Properties["Fax"] != null)
       {
          string fax = result.Properties["Fax"][0].ToString();
       }
    
       if (result.Properties["Mobile"] != null)
       {
          string mobile = result.Properties["Mobile"][0].ToString();
       }
    }
    

    【讨论】:

    • 成功了!万分感谢!只需将“传真”替换为“传真电话号码”。有什么方法可以指定在过滤器中返回哪些属性(或所有属性)?
    • @user1016945:不,指定要返回哪些属性的位置是PropertiesToLoad - 没有别的。
    • 不需要加[0]吗? result.Properties["Fax"][0].ToString()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 2012-12-30
    • 1970-01-01
    • 2015-08-23
    • 2013-12-14
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多