Beispiel #1
0
        public void TestCase()
        {
            var result = new List <LdapUser>();

            var userContainerSettings = LdapConfigurationsHelper.GetUserContainerSettings();

            if (userContainerSettings == null)
            {
                return;
            }

            foreach (var item in userContainerSettings)
            {
                using (var connection = new LdapConnection(item.Path))
                {
                    var service = new LdapUserService(connection);

                    var list = service.Search(new LdapSearchParameters
                    {
                        PropertiesToLoad = new[] { "cn", "department", "givenname", "mail", "manager", "mobile", "samaccountname", "sn", "streetaddress", "telephonenumber", "title", "useraccountcontrol" },
                        Filter           = "(sn=*)", //search users which surname is not empty
                        SortOption       = new SortOption {
                            Direction = SortDirection.Ascending, PropertyName = "samaccountname"
                        }
                    }).ToList <LdapUser>();

                    result.AddRange(list);
                }
            }
        }
Beispiel #2
0
        public LdapConnection(string container)
        {
            var settings = LdapConfigurationsHelper.GetSettings();

            Settings = settings ?? throw new ConfigurationErrorsException("Please check your LdapConfiguration on app / web.config");

            Username = settings.Username;
            Password = settings.Password;

            Context = new PrincipalContext(ContextType.Domain, settings.Server.Replace("LDAP://", ""), container, string.IsNullOrEmpty(settings.Domain) ? Username : settings.Domain + "\\" + settings.Username, Password);

            if (!ValidateCredentials())
            {
                Context = null;
                throw new InvalidCredentialException("Invalid credentials! Please check your configurations.");
            }

            var server = Settings.Server;

            if (!server.StartsWith("LDAP://"))
            {
                server = string.Concat("LDAP://", server);
            }

            DirectoryEntry = new DirectoryEntry($"{server}/{container}", string.IsNullOrEmpty(settings.Domain) ? Username : settings.Domain + "\\" + settings.Username, Password, (AuthenticationTypes)Enum.Parse(typeof(AuthenticationTypes), settings.AuthenticationType, true));
        }