AD与LDAP连接错误

时间:2015-01-14 11:28:08

标签: visual-studio-2010 active-directory ldap windows-server-2003

我试图使用.net应用程序,但应用程序无法在我的本地网络中找到服务器。

我使用LdapExploreTool 2进行以下设置: LdapExplorer

the base DN is "DC=exago,DC=local", the Ip address "192.168.1.250" and the server name "exago.local"

连接成功,结果如下: ldap
输入值: ldap2
检查代码时,“绑定到本机AdsObject以强制进行身份验证”时出现异常:

“指定的域名不存在或无法联系。”

  public bool IsAuthenticated(string domain, string ldapPath, string username, string pwd, string userToValidate)
    {
        string domainAndUsername = domain + @"\" + username;

        if (string.IsNullOrEmpty(ldapPath))
            SetLdapPath(domain);
        else
            _path = ldapPath;
        App.Services.Log.LogUtils.WriteLog(Log.LogLevel.INFO, "IsAuthenticated_DirectoryEntry:" + _path + "," + domainAndUsername + "," + pwd);
        DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);

        //check if domain is valid
        int domainId = AppDomains.GetDomainIdByName(domain);
        if (domainId == int.MinValue)
        {
            return false;
        }

        AppDomains d = AppDomains.GetRecord(domainId);
        List<AppDomainQueries> lQueries = new List<AppDomainQueries>(AppDomainQueries.GetArray());
        lQueries = lQueries.FindAll(delegate(AppDomainQueries dq) { return dq.DomainId == domainId && dq.Status == 'A'; });

        string queryString = string.Empty;
        try
        {
            // Bind to the native AdsObject to force authentication.
            Object obj = entry.NativeObject;

            DirectorySearcher search = new DirectorySearcher(entry);

            string ldapAndQuerie = string.Empty;

            //base account search
            queryString = "(SAMAccountName=" + userToValidate + ")";


            if (username != userToValidate)
            {
                if (lQueries.Count == 1)
                    ldapAndQuerie = lQueries.FirstOrDefault().QueryString;

                if ((ldapAndQuerie != string.Empty) && (ldapAndQuerie != "*") && (ldapAndQuerie != "(objectClass = user)"))
                    queryString = "(&(SAMAccountName=" + userToValidate + ")" + ldapAndQuerie + ")";
            }

            search.Filter = queryString;

            App.Services.Log.LogUtils.WriteLog(Log.LogLevel.INFO, "LDAP=" + queryString);


            search.PropertiesToLoad.Add("cn");
            SearchResult result = search.FindOne();
            if (null == result)
            {
                return false;
            }
            // Update the new path to the user in the directory
            _path = result.Path;
            _filterAttribute = (String)result.Properties["cn"][0];
        }
        catch (Exception ex)
        {
            App.Services.Log.LogUtils.WriteLog(Log.LogLevel.ERROR, "App.Services.Core.LdapAuthentication.IsAuthenticated() Exception - (LDAP=" + queryString + ")" + ex.Message, ex);
            return false;
        }
        return true;
    }

如何建立连接?

1 个答案:

答案 0 :(得分:0)

问题是LDAP连接字符串, 它似乎错过了网络中的实际位置(IP +端口)。

LDAP://192.168.1.250:389/DC=exago,DC=local