了解使用C#代码模拟它们的LDAP响应



我需要模拟一个LDAP目录,以向需要LDAP工作的一个应用程序发送预定义的响应。我正在使用LDAP浏览器和LDAP管理员进行一些测试,然后收听389端口。要编写响应,我将使用网络流和流动者。从LDAP RFC i有以下模型以进行搜索响应。我不知道如何构建这个信封,我必须构建一个字节序列吗?

SearchResultEntry ::= [APPLICATION 4] SEQUENCE {
                objectName      LDAPDN,
                 attributes      PartialAttributeList }
        PartialAttributeList ::= SEQUENCE OF SEQUENCE {
                type    AttributeDescription,
                vals    SET OF AttributeValue }
        -- implementors should note that the PartialAttributeList may
        -- have zero elements (if none of the attributes of that entry
        -- were requested, or could be returned), and that the vals set
        -- may also have zero elements (if types only was requested, or
        -- all values were excluded from the result.)
        SearchResultReference ::= [APPLICATION 19] SEQUENCE OF LDAPURL
        -- at least one LDAPURL element must be present
        SearchResultDone ::= [APPLICATION 5] LDAPResult

我收到了客户端正在发送的请求,我可以识别请求的字符串部分,但是我没有找到正确回答的正确方法。通过在请求中找到的字符串,我可以弄清楚客户端要发送的消息。

public static void Connect()
{
        try
        {
            int port = 389;
            IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
            TcpListener listener = new TcpListener(ipAddress, port);
            listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            listener.Start();
            while (true)
            {
                LDAPLayer handler = new LDAPLayer(listener.AcceptTcpClient());
                Thread thread = new Thread(new ThreadStart(handler.LDAPListener));
                thread.Start();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            client.Close();
        }
}
public void LDAPListener()
{
        try
        {
            while (true)
            {
                string line = reader.ReadLine();
                string date = DateTime.Now.ToString("yyyyMMdd_HHmmss");
                while (line != null)
                {
                    Console.WriteLine(line);
                    if (line.Contains("objectClass"))
                    {
                        writer.Write(0);
                    }
                    line = reader.ReadLine();
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.Message);
        }
}

ldap不是文本协议。每个LDAP消息必须根据X.690规格使用BER规则进行编码。

最新更新