活动目录 findone() 方法

发布于 2024-12-12 01:04:58 字数 1035 浏览 1 评论 0原文

我试图使用这一行查询广告,

            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()

im trying to inquire Ad by using this line`s

            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();

but im getting an exception in the 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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绳情 2024-12-19 01:04:58

未指定的错误意味着您的 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(); 

希望这会有所帮助。

Unspecified error means, that your LDAP path is missing the LDAP protocol identifier.
Ensure that your path contains the LDAP protocol identifier in upper case.

Example:

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(); 

Hope, this helps.

苏大泽ㄣ 2024-12-19 01:04:58
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 字符串,我建议使用语句来确保您随后处理掉所有内容。

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;
    }

Note: The string.Concat() around the "(objectClass=*)" statement is there because It's common to add additional statements or variables there.

Make sure you have a proper LDAP string, and I would suggest using statements to make sure you dispose of everything afterwards.

南笙 2024-12-19 01:04:58

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

de.path=dr.dr.manager   

当我在语句中添加“LDAP://”时,

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

,它解决了这个问题,感谢分配的支持

acutely my mistake was much more basic than the exception mentioned ..
i wrote this wrong active directory statement

de.path=dr.dr.manager   

when i added "LDAP://" to the statement it solve it

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

thanks allot for the support

哆兒滾 2024-12-19 01:04:58

尝试这种方式:

/* 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);

}

在这种情况下,我以用户和密码身份进行身份验证。如果您从域内的计算机运行该程序,则不需要进行身份验证。 此处提供了一个很好的示例。

try yhis way :

/* 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);

}

In this case I authenticate as a user and a password. If you run the program from a computer inside the domain, you don't need to authenticate. You've got a good sample here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文