【问题标题】:AD Query, Server is not operationalAD 查询,服务器无法运行
【发布时间】:2012-04-03 11:35:23
【问题描述】:

所以,我有这个方法,在最后一行,它试图获取 samAccountName,它向我抛出一个 COM 异常,这让我抓狂。

有什么想法吗?

public User FindUsername(string samAccountName, string groupDisplayName)   
        {
            using (DirectoryEntry searchRoot = new DirectoryEntry(ldapf, ldapu, ldapp))
            {
                using (DirectorySearcher searcher = new DirectorySearcher(searchRoot))
                {               
                    searcher.Asynchronous = false;
                    searcher.PropertiesToLoad.Add("SAMAccountName");
                    searcher.PropertiesToLoad.Add("displayName");
                    searcher.PropertiesToLoad.Add("uSNChanged");
                    searcher.PropertiesToLoad.Add("member");
                    searcher.PropertiesToLoad.Add("co");
                    searcher.PropertiesToLoad.Add("company");
                    searcher.PropertiesToLoad.Add("mail");


                    searcher.Filter = String.Format("(SAMAccountName={0})", samAccountName);
                    searcher.SearchScope = SearchScope.Subtree;
                    searcher.PageSize = 1000;

                    SearchResult result = searcher.FindOne();
                    ResultPropertyCollection resultPropColl = result.Properties;
                    Object memberColl = resultPropColl["member"];
                    using (DirectoryEntry memberEntry = new DirectoryEntry("LDAP://" + memberColl, ldapu, ldapp))
                    {
                        try
                        {
                            System.DirectoryServices.PropertyCollection userprops = memberEntry.Properties;
                            object obVal = userprops["SAMAccountName"].Value;

【问题讨论】:

标签: c# active-directory directoryservices


【解决方案1】:

我注意到我的代码在其他地方运行时出现问题,我缺少一个 foreach

foreach (Object memberColl in resultPropColl["member"])
                    {

显然如果它返回一个用户,resultPropColl["member"] 仍然是一个对象的集合?

【讨论】:

    猜你喜欢
    • 2017-08-11
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 2019-09-30
    • 2014-07-31
    • 2020-12-09
    • 2012-08-21
    相关资源
    最近更新 更多