【问题标题】:Unable to connect to LDAP无法连接到 LDAP
【发布时间】:2016-01-09 20:39:50
【问题描述】:

我在this question 中读到,在上述问题中也涵盖的 0x 错误的常见答案通常是指定要搜索的帐户。

我意识到我已经在这样做了——事实上,我正在尝试使用活动目录来对我的应用程序的用户进行身份验证。最初我认为我的错误问题与我如何制定搜索参数有关:

string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);

_path 返回了 URI。 domainAndUsername 返回以下内容:

"domain.com\\usrname"

所以我将实例化改为:

DirectoryEntry entry = new DirectoryEntry(_path, username, pwd);

返回“usr@domain.com”(我替换为“@domain.com”,因为我的实现需要用户输入他们的电子邮件地址和 NT 密码)。

这使我考虑到我的测试可能是在一个没有足够特权执行目录搜索的帐户下执行的可能性很小 - 但我已经使用另一个实现搜索了相同的数据 - 在同一个项目,不少

那么,为什么每当我尝试使用object obj = entry.NativeObject; 时,我的实现会因0x80005000 而失败?

更重要的是,为什么 这个 实现会失败,而我原来的实现却没有?作为参考,此实现使用与 Microsoft here 演示的相同格式,我的原始(工作)实现如下:

public class dirSearch
{
    public bool searchSuccessful;
    public string errStr;

    List<string> resList = new List<string>();
    public void getEmpDetails(string filStr, string varStr)
    {
        string strServerDNS = "ldap.domain.com:389";
        string strSearchBaseDN = "ou=People,o=domain.com";
        string strLDAPPath = "LDAP://" + strServerDNS + "/" + strSearchBaseDN;
        DirectoryEntry objDirEntry = new DirectoryEntry(strLDAPPath, null, null, AuthenticationTypes.Anonymous);
        DirectorySearcher searcher = new DirectorySearcher(objDirEntry);
        SearchResultCollection results;

        searcher.Filter = "(uid=" + filStr + ")";
        //make sure the order of the search is like so:
        //UID
        //empnum
        //full name
        searcher.PropertiesToLoad.Add(varStr);

        try
        {
            results = searcher.FindAll();
            foreach (SearchResult result in results)
            {
                string temStr = result.Properties[varStr][0].ToString();
                resList.Add(temStr);
                searchSuccessful = true;
            }
        }
        catch (Exception e)
        {
            errStr = e.ToString();
            searchSuccessful = false;
        }
    }
}

更多信息:

我从异常中提取了错误代码,恰好是 -2147463168。这对应于ADS_BAD_PATHNAME。这有点令人困惑,因为 LDAP://ldap.domain.com:389/ 是正确的。我的理论是我应该使用专有名称,在这种情况下,我需要返回我原来的工作方法并提取工作人员的全名,然后再用该信息制定名称。

【问题讨论】:

    标签: c# active-directory directoryservices


    【解决方案1】:

    我注意到您使用匿名身份验证来登录您的 AD,我想您想使用用户名和密码,然后查看用户是否存在。试试下面的代码:

        public bool IsAuthenticated(string username, string pwd)
        {
            string strLDAPServerAndPort = "ldap.domain.com:389";
            string strLDAPContainer = "CN=Users,DC=domain,DC=com";
    
    
            string strLDAPPath = "LDAP://" + strLDAPServerAndPort + "/" + strLDAPContainer;
            DirectoryEntry objDirEntry = new DirectoryEntry(strLDAPPath, username, pwd);
    
            try
            {
                //Bind to the native AdsObject to force authentication.
                object obj = objDirEntry.NativeObject;
    
                DirectorySearcher search = new DirectorySearcher(objDirEntry);
    
                search.Filter = "(SAMAccountName=" + username + ")";
                search.PropertiesToLoad.Add("cn");
                SearchResult result = search.FindOne();
    
                if (null == result)
                {
                    return false;
                }
    
            }
            catch (Exception ex)
            {
                throw new Exception("Error authenticating user. " + ex.Message);
            }
    
            return true;
    
        }
    

    【讨论】:

    • 请记住,我的问题底部的示例是工作实现 - 也就是说,我可以返回电子邮件地址、员工编号和全名。我还尝试只指定一个专有名称,这导致了无效的 DN 语法 - 与您的代码相同的结果。但是,我不确定该错误是否归因于无效的登录尝试或无效的搜索尝试。
    • 我建议你下载活动目录资源管理器,它会为你提供你要搜索的正确路径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多