Exemple #1
0
        private static IDirectoryAttributes AddContainerIfNecessary(Type type)
        {
            var mapping = LdapConfiguration.Configuration.Mapper.GetMapping(type);

            var containerCn     = DnParser.ParseName(mapping.NamingContext);
            var containerPrefix = DnParser.ParseRDN(mapping.NamingContext);

            return(AddContainerIfNecessary(containerPrefix, containerCn));
        }
Exemple #2
0
        private static void PopulateDirectoryForTests()

        {
            var user2Container = DnParser.ParseName(TestUserDirectoryContainer);

            var user2Prefix = DnParser.ParseRDN(TestUserDirectoryContainer);

            AddContainerIfNecessary(user2Prefix, user2Container);

            using (var context = new DirectoryContext())

            {
                AddEntryIfNecessary("CN=PasswordUser," + TestUserDirectoryContainer, "user", context);

                AddEntryIfNecessary("CN=persontest," + TestUserDirectoryContainer, "user", context);

                AddEntryIfNecessary("CN=TestUser," + TestUserDirectoryContainer, "user", context);

                AddEntryIfNecessary("CN=TestUser2," + TestUserDirectoryContainer, "user", context);
            }

            var roleContainer = DnParser.ParseName(RolesDirectoryContainer);

            var rolePrefix = DnParser.ParseRDN(RolesDirectoryContainer);

            AddContainerIfNecessary(rolePrefix, roleContainer);

            using (var context = new DirectoryContext())

            {
                IDirectoryAttributes rangeTest = AddEntryIfNecessary("CN=RangeTest," + RolesDirectoryContainer, "group", context);

                foreach (var kvp in rangeTest.Where(kvp => kvp.Key.StartsWith("member", StringComparison.OrdinalIgnoreCase)))

                {
                    if (kvp.Value is IEnumerable <string> && ((IEnumerable <string>)kvp.Value).Any())

                    {
                        Console.WriteLine("RangeTest members already populated");

                        return;
                    }
                }

                var newMembers = new List <string>();

                newMembers.AddRange(context.Query <LdsUser>().Take(10000).Select(u => u.DistinguishedName));

                rangeTest.Set("member", newMembers);

                context.Update(rangeTest);
            }
        }
Exemple #3
0
        private static void EnablePasswordChangesOn389()

        {
            using (var context = new DirectoryContext())

            {
                var attributes = context.ListServerAttributes("namingcontexts");

                var namingContexts = attributes.GetStrings("namingcontexts");

                var configurationDN = namingContexts != null

                                          ? namingContexts

                                      .FirstOrDefault(s => DnParser.ParseName(s)

                                                      .Equals("Configuration", StringComparison.OrdinalIgnoreCase))

                                          : null;

                bool success = false;

                if (configurationDN != null)

                {
                    var directoryService = context.Query(configurationDN)

                                           .Where(_ => Filter.Equal(_, "cn", "Directory Service", true))

                                           .Select("distinguishedName")

                                           .FirstOrDefault();

                    if (directoryService != null)

                    {
                        directoryService.Set("dSHeuristics", "0000000001001");

                        context.Update(directoryService);

                        success = true;
                    }
                }

                if (!success)

                {
                    Console.WriteLine("Could not set a password policy");
                }
            }
        }
        /// <summary>
        /// Renames the entry within the same container. The <paramref name="newName"/> can be in the format
        /// XX=New Name or just New Name.
        /// </summary>
        /// <param name="connection">The connection to the directory.</param>
        /// <param name="log">The log for query information. Defaults to null.</param>
        /// <param name="currentDistinguishedName">The entry's current distinguished name</param>
        /// <param name="newName">The new name of the entry</param>
        /// <param name="deleteOldRDN">Maps to <see cref="P:System.DirectoryServices.Protocols.ModifyDNRequest.DeleteOldRdn"/>. Defaults to null to use default behavior</param>
        /// <param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param>
        /// <param name="resultProcessing">How the async results are processed</param>
        /// <exception cref="ArgumentException">
        /// Thrown if <paramref name="currentDistinguishedName"/> has an invalid format.
        /// </exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="currentDistinguishedName"/>
        /// or <paramref name="newName"/> are null, empty or white space.
        /// </exception>
        /// <exception cref="DirectoryOperationException">Thrown if the operation fails.</exception>
        /// <exception cref="LdapConnection">Thrown if the operation fails.</exception>
        public static async Task <string> RenameEntryAsync(this LdapConnection connection, string currentDistinguishedName, string newName, ILinqToLdapLogger log = null,
                                                           bool?deleteOldRDN = null, DirectoryControl[] controls = null, PartialResultProcessing resultProcessing = LdapConfiguration.DefaultAsyncResultProcessing)
        {
            try
            {
                if (connection == null)
                {
                    throw new ArgumentNullException("connection");
                }

                if (currentDistinguishedName.IsNullOrEmpty())
                {
                    throw new ArgumentNullException("currentDistinguishedName");
                }

                if (newName.IsNullOrEmpty())
                {
                    throw new ArgumentNullException("newName");
                }

                newName = DnParser.FormatName(newName, currentDistinguishedName);
                var container = DnParser.GetEntryContainer(currentDistinguishedName);

                var response = await SendModifyDnRequestAsync(connection, currentDistinguishedName, container, newName, deleteOldRDN, controls, log, resultProcessing).ConfigureAwait(false);

                response.AssertSuccess();

                return(string.Format("{0},{1}", newName, container));
            }
            catch (Exception ex)
            {
                if (log != null)
                {
                    log.Error(ex, string.Format("An error occurred while trying to rename entry '{0}' to '{1}'.", currentDistinguishedName, newName));
                }

                throw;
            }
        }
Exemple #5
0
        /// <summary>
        /// Renames the entry within the same container. The <paramref name="newName"/> can be in the format
        /// XX=New Name or just New Name.
        /// </summary>
        /// <param name="connection">The connection to the directory.</param>
        /// <param name="log">The log for query information. Defaults to null.</param>
        /// <param name="currentDistinguishedName">The entry's current distinguished name</param>
        /// <param name="newName">The new name of the entry</param>
        /// <param name="deleteOldRDN">Maps to <see cref="P:System.DirectoryServices.Protocols.ModifyDNRequest.DeleteOldRdn"/>. Defaults to null to use default behavior</param>
        /// <param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param>
        /// <exception cref="ArgumentException">
        /// Thrown if <paramref name="currentDistinguishedName"/> has an invalid format.
        /// </exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="currentDistinguishedName"/>
        /// or <paramref name="newName"/> are null, empty or white space.
        /// </exception>
        /// <exception cref="DirectoryOperationException">Thrown if the operation fails.</exception>
        /// <exception cref="LdapConnection">Thrown if the operation fails.</exception>
        public static string RenameEntry(this LdapConnection connection, string currentDistinguishedName, string newName, ILinqToLdapLogger log = null, bool?deleteOldRDN = null, params DirectoryControl[] controls)
        {
            try
            {
                if (connection == null)
                {
                    throw new ArgumentNullException("connection");
                }

                if (currentDistinguishedName.IsNullOrEmpty())
                {
                    throw new ArgumentNullException("currentDistinguishedName");
                }

                if (newName.IsNullOrEmpty())
                {
                    throw new ArgumentNullException("newName");
                }

                newName = DnParser.FormatName(newName, currentDistinguishedName);
                var container = DnParser.GetEntryContainer(currentDistinguishedName);

                var response = SendModifyDnRequest(connection, currentDistinguishedName, container, newName, deleteOldRDN, controls, log);
                response.AssertSuccess();

                return(string.Format("{0},{1}", newName, container));
            }
            catch (Exception ex)
            {
                if (log != null)
                {
                    log.Error(ex, string.Format("An error occurred while trying to rename entry '{0}' to '{1}'.", currentDistinguishedName, newName));
                }

                throw;
            }
        }
Exemple #6
0
 public void ParseRDN_MultipleCharacters_ReturnsPrefix()
 {
     DnParser.ParseRDN("XYZ=test,Dd=domain,Dd=com").Should().Be.EqualTo("XYZ");
 }
Exemple #7
0
 public void ParseRDN_SingleCharacter_ReturnsPrefix()
 {
     DnParser.ParseRDN("n=test,Dd=domain,Dd=com").Should().Be.EqualTo("n");
 }
Exemple #8
0
 public void GetEntryContainer_OU_ReturnsContainer()
 {
     DnParser.GetEntryContainer("OU=test,Cn=test2,Dc=domain,Dc=com").Should().Be.EqualTo("Cn=test2,Dc=domain,Dc=com");
 }
Exemple #9
0
 public void GetEntryContainer_OneRDN_ReturnsOU()
 {
     DnParser.GetEntryContainer("OU=DoeJohn").Should().Be.EqualTo("OU=DoeJohn");
 }
Exemple #10
0
 public void GetEntryContainer_RDNWithComma_ReturnsOU()
 {
     DnParser.GetEntryContainer("OU=Doe, John,Cn=test2,Dc=domain,Dc=com").Should().Be.EqualTo("Cn=test2,Dc=domain,Dc=com");
 }
Exemple #11
0
 public void ParseRDN_NoEqualsIndex_ThrowsException()
 {
     Executing.This(() => DnParser.ParseName("Test"))
     .Should().Throw <ArgumentException>();
 }
Exemple #12
0
 public void GetEntryName_CN_ReturnsCN()
 {
     DnParser.GetEntryName("Cn=test,Dd=domain,Dd=com").Should().Be.EqualTo("Cn=test");
 }
Exemple #13
0
        public void FormatName_WithPrefix_DoesNotAppendsPrefix()
        {
            var formatted = DnParser.FormatName("CN=t", "Cn=test,Dc=domain,Dc=com");

            formatted.Should().Be.EqualTo("CN=t");
        }
Exemple #14
0
 public void ParseName_CN_ReturnsCN()
 {
     DnParser.ParseName("Cn=test,Dc=domain,Dc=com").Should().Be.EqualTo("test");
 }
Exemple #15
0
 public void ParseName_OneRDN_ReturnsOU()
 {
     DnParser.ParseName("OU=DoeJohn").Should().Be.EqualTo("DoeJohn");
 }
Exemple #16
0
 public void ParseRDN_NullDn_ThrowsException()
 {
     Executing.This(() => DnParser.ParseRDN(null))
     .Should().Throw <ArgumentException>();
 }
Exemple #17
0
 public void ParseRDN_BadEqualsIndex_ThrowsException()
 {
     Executing.This(() => DnParser.ParseName("=test,Cn=test2,Dc=domain,Dc=com"))
     .Should().Throw <ArgumentException>();
 }
Exemple #18
0
 public void GetEntryName_OU_ReturnsOU()
 {
     DnParser.GetEntryName("OU=test,Cn=test2,Dc=domain,Dc=com").Should().Be.EqualTo("OU=test");
 }
Exemple #19
0
 public void ParseName_SingleCharacter_ReturnsCN()
 {
     DnParser.ParseName("n=test,Dd=domain,Dd=com").Should().Be.EqualTo("test");
 }
Exemple #20
0
 public void GetEntryName_OneRDN_ReturnsOU()
 {
     DnParser.GetEntryName("OU=DoeJohn").Should().Be.EqualTo("OU=DoeJohn");
 }