Exemple #1
0
        private void AddLdapEntry()
        {
            using (var connection = new LdapConnection())
            {
                connection.Connect(Config.LdapHost, Config.LdapPort);
                connection.Bind(LdapAuthMechanism.SIMPLE, Config.LdapUserDn, Config.LdapPassword);
                var ldapEntry = new LdapEntry
                {
                    Dn         = $"cn=test,{Config.RootDn}",
                    Attributes = new Dictionary <string, List <string> >
                    {
                        { "cn", new List <string> {
                              "test"
                          } },
                        { "sn", new List <string> {
                              "Winston"
                          } },
                        { "objectclass", new List <string> {
                              "inetOrgPerson", "top"
                          } },
                        { "givenname", new List <string> {
                              "винстон"
                          } },
                        { "description", new List <string> {
                              "test_value"
                          } }
                    }
                };
                var directoryEntry = ldapEntry.ToDirectoryEntry();
                var image          = new DirectoryAttribute
                {
                    Name = "jpegPhoto"
                };
                image.Add(new byte[] { 1, 2, 3, 4 });
                directoryEntry.Attributes.Add(image);
                var response = (AddResponse)connection.SendRequest(new AddRequest(directoryEntry.Dn, directoryEntry.Attributes.ToArray()));
                Assert.True(response.ResultCode == 0);

                var entries = connection.Search(Config.RootDn, "(&(objectclass=top)(cn=test))");
                Assert.True(entries.Count == 1);
                _testOutputHelper.WriteLine(entries[0].Dn);

                Assert.Equal($"cn=test,{Config.RootDn}", entries[0].Dn);
                Assert.Equal("винстон", GetAttributeValue(entries[0].Attributes, "givenName")[0]);
                Assert.True(GetAttributeValue(entries[0].Attributes, "objectClass").Any());
            }
        }