AD Query,服务器无法运行

本文关键字:运行 服务器 Query AD | 更新日期: 2023-09-27 18:29:55

所以,我有这个方法,在最后一行,它试图获取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;

AD Query,服务器无法运行

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

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

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