无法连接到LDAP

时间:2015-10-12 11:25:23

标签: c# active-directory directoryservices

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

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

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密码。)

这让我想到我的测试可能会在一个没有特权进行目录搜索的帐户下执行的可能性很小 - 但我已经用另一个实现搜索了相同的数据 - 在同一个项目中,不亚于

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

更重要的是,为什么这个实现在我的原始实现没有时会失败?作为参考,此实现使用的格式与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 /是正确的。我的理论是我应该使用专有名称,在这种情况下,我需要返回原来的工作方法并提取工作人员的全名,然后再使用该信息制定名称。

1 个答案:

答案 0 :(得分:0)

我注意到您使用匿名身份验证登录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;

    }