连接到Open LDAP无法与System.DirectoryServices.Protocols绑定

时间:2014-10-06 16:07:00

标签: binding openldap directoryservices

我一直在尝试使用MSDN中的示例代码连接到Open LDAP(Alex Tcherniakhovski)

http://blogs.msdn.com/b/alextch/archive/2012/05/07/sample-code-to-query-openldap-directory-via-net-system-directoryservices-protocols.aspx

我尝试过PORT 636:ssl,因为它在示例代码中

在PORT 389非ssl看我是否能成功

尝试使用PORT 389时(使用相同的凭据,我可以使用Softerra LDAP浏览器连接到OPEN LDAP)

我收到以下错误:专有名称包含无效语法。

我运行了Microsoft网络监视器,发现在我的名字之前,我的Bind请求中添加了一些不需要的字符。这些字符从未出现在dotnet解决方案中,但它们是请求的一部分并使其失败。

你知道如何摆脱这些吗?

我会看到一张图片,但我不被允许。

我的监视器显示BindRequest:版本:3,名称:âcn= Manager,dc = ...

dotnet代码名称中的

是“cn = Manager,dc = ..”

在端口636上使用SSL上的代码会导致以下错误:LDAP服务器不可用。

尝试从此处下载的Solution DirectoryServices.Protocol中连接sslbind时遇到同样的错误。

http://www.microsoft.com/en-us/download/confirmation.aspx?id=18086

感谢您的帮助

using System.Collections.Generic;
using System.DirectoryServices.Protocols;
using System.Globalization;
using System.Net;
using System.Security;



namespace OpenLDAPNextUID
{
public class LDAPHelper
{
    private readonly LdapConnection ldapConnection;
    private readonly string searchBaseDN;
    private readonly int pageSize;

    public LDAPHelper(
        string searchBaseDN,
        string hostName,
        int portNumber,
        AuthType authType,
        string connectionAccountName,
        SecureString connectionAccountPassword,
        int pageSize)
    {

        var ldapDirectoryIdentifier = new LdapDirectoryIdentifier(
            hostName,
            portNumber,
            true,
            false);

        var networkCredential = new NetworkCredential(
            connectionAccountName,
            connectionAccountPassword);

        ldapConnection = new LdapConnection(
            ldapDirectoryIdentifier,
            networkCredential) 
            {AuthType = authType};

        ldapConnection.SessionOptions.ProtocolVersion = 3;

        this.searchBaseDN = searchBaseDN;
        this.pageSize = pageSize;
    }

    public IEnumerable<SearchResultEntryCollection> PagedSearch(
        string searchFilter,
        string[] attributesToLoad)
    {

        var pagedResults = new List<SearchResultEntryCollection>();

        var searchRequest = new SearchRequest
                (searchBaseDN,
                 searchFilter,
                 SearchScope.Subtree,
                 attributesToLoad);


        var searchOptions = new SearchOptionsControl(SearchOption.DomainScope);
        searchRequest.Controls.Add(searchOptions);

        var pageResultRequestControl = new PageResultRequestControl(pageSize);
        searchRequest.Controls.Add(pageResultRequestControl);

        while (true)
        {
            var searchResponse = (SearchResponse)ldapConnection.SendRequest(searchRequest);
            var pageResponse = (PageResultResponseControl)searchResponse.Controls[0];

            yield return searchResponse.Entries;
            if (pageResponse.Cookie.Length == 0)
                break;

            pageResultRequestControl.Cookie = pageResponse.Cookie;
        }


    }
}
}




namespace OpenLDAP
{
    class Program
    {
        static void Main(string[] args)
        {
            var password = new[]{'P','a','s','s','w','@','r','d'};
            var secureString = new SecureString();
            foreach (var character in password)
                secureString.AppendChar(character);

            var baseOfSearch = "dc=fabrikam,dc=com";
            var ldapHost = "ubuntu.fabrikam.com";
            var ldapPort = 636; //SSL
            var ldapPort = 389; //not SSL
            var connectAsDN = "cn=admin,dc=fabrikam,dc=com";
            var pageSize = 1000;

            var openLDAPHelper = new LDAPHelper(
                baseOfSearch,
                ldapHost,
                ldapPort,
                AuthType.Basic, 
                connectAsDN,
                secureString,
                pageSize);

            var searchFilter = "nextUID=*";
            var attributesToLoad = new[] {"nextUID"};
            var pagedSearchResults = openLDAPHelper.PagedSearch(
                searchFilter,
                attributesToLoad);

            foreach (var searchResultEntryCollection in pagedSearchResults)
                foreach (SearchResultEntry searchResultEntry in searchResultEntryCollection)
                    Console.WriteLine(searchResultEntry.Attributes["nextUID"][0]);

            Console.Read();

        }
    }
}

0 个答案:

没有答案