从c#访问活动目录

本文关键字:活动 访问 | 更新日期: 2023-09-27 18:04:30

我试图从我的控制台应用程序访问XXX域中的目录。

             DirectoryEntry oDE = new DirectoryEntry("LDAP://DC=XXXX,DC=myDomain,DC=com");
            using (DirectorySearcher ds = new DirectorySearcher(oDE))
            {
                ds.PropertiesToLoad.Add("name");
                ds.PropertiesToLoad.Add("userPrincipalName");
                ds.Filter = "(&(objectClass=user))";
                SearchResultCollection results = ds.FindAll();
                foreach (SearchResult result in results)
                {
                    Console.WriteLine("{0} - {1}",
                        result.Properties["name"][0].ToString(),
                        result.Properties["userPrincipalName"][0].ToString());
                }
            }

当行SearchResultCollection results = ds.FindAll();执行时收到错误"服务器上没有这样的对象"。

我做错了什么?

从c#访问活动目录

好的,在评论中简短地回顾一下我们的谈话:

您当前的问题是由于您没有正确格式化LDAP uri而导致的。

LDAP URI建立=" LDAP://DC="

后面跟着你的服务器uri(例如Test1.Test2.gov.lk),在这里你替换了'。' with ',DC='

因此,Test1.Test2.gov.lk变成'LDAP://DC=Test1,DC=Test2,DC=gov,DC=lk'

我不能帮你解决你的后续问题;我建议为此创建一个新问题。

祝你好运,尼克。