活动目录findone()方法

时间:2011-10-25 16:37:57

标签: c# active-directory

我试图通过使用此行来查询广告

            DirectoryEntry de = null;
            SearchResult results = null;
            de = new DirectoryEntry();

            //geting the result FROM ad
            de.Path = dr.manager;
            de.AuthenticationType = AuthenticationTypes.Secure;
            DirectorySearcher search = new DirectorySearcher(de);
            search.Filter = string.Format("(objectClass={0})",'*');
            search.PropertiesToLoad.Add("IsraelID");
            results = search.FindOne();
            de = results.GetDirectoryEntry();

但我在findone()

中获得了一个例外
System.Runtime.InteropServices.COMException (0x80004005): Unspecified error

   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_AdsObject()
   at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
   at System.DirectoryServices.DirectorySearcher.FindOne()

4 个答案:

答案 0 :(得分:8)

未指定的错误表示您的LDAP路径缺少LDAP协议标识符。 确保您的路径包含大写的LDAP协议标识符。

示例:

DirectoryEntry de = null; 
SearchResult results = null; 
de = new DirectoryEntry(); 

// Assuming your domain dns name is treyresearch.net 
de.Path = "LDAP://servername/CN=users,DC=treyresearch,DC=net"; 
de.AuthenticationType = AuthenticationTypes.Secure; 
de.Username = "treyresearch\\Administrator";
de.Password = "P@$$W0rd";
DirectorySearcher search = new DirectorySearcher(de); 
search.Filter = string.Format("(objectClass={0})",'*'); 
search.PropertiesToLoad.Add("IsraelID"); 
results = search.FindOne(); 
de = results.GetDirectoryEntry(); 

希望,这有帮助。

答案 1 :(得分:2)

string LDAP = "LDAP://DC=MYDOMAIN,DC=COM";
using (DirectoryEntry dirEntry = new DirectoryEntry(LDAP, null, null, AuthenticationTypes.Secure))
    using (DirectorySearcher dirSearch = new DirectorySearcher(
        dirEntry,
        string.Concat("(objectClass=*)"),
        new string[] { "IsraelID" }))
    {
        SearchResult result = dirSearch.FindOne();
        if (result != null)
            return result.Properties["IsraelID"][0].ToString();
        else
            return null;
    }

注意:“(objectClass = *)”语句周围的string.Concat()是存在的,因为在那里添加其他语句或变量是很常见的。

确保您拥有正确的LDAP字符串,我建议您使用语句确保事后处理完所有内容。

答案 2 :(得分:1)

我的错误比我提到的例外更为基本。 我写了这个错误的活动目录语句

de.path=dr.dr.manager   

当我将“LDAP://”添加到它解决它的语句

de.Path = "LDAP://"+dr.manager;

感谢分配支持

答案 3 :(得分:0)

试试这种方式:

/* Connection to Active Directory
 */
DirectoryEntry deBase = new DirectoryEntry("LDAP://WM2008R2ENT:389/dc=dom,dc=fr", "jpb", "Pwd");
//DirectoryEntry deBase = new DirectoryEntry("LDAP://WM2008R2ENT:389/dc=dom,dc=fr");

/* Directory Search
 */
DirectorySearcher dsLookForOUs = new DirectorySearcher(deBase);
dsLookForOUs.Filter = "(objectCategory=organizationalUnit)";
dsLookForOUs.SearchScope = SearchScope.Subtree;
dsLookForOUs.PropertiesToLoad.Add("cn");
dsLookForOUs.PropertiesToLoad.Add("ou");

SearchResultCollection srcOUs = dsLookForOUs.FindAll();

foreach (SearchResult srOU in srcOUs)
{
  Console.WriteLine("{0}", srOU.Path);

}

在这种情况下,我作为用户和密码进行身份验证。如果从域内的计算机运行该程序,则无需进行身份验证。你有一个很好的样本here