Esempio n. 1
0
 private void SetLdapControls(LdapFilter pageInfo, LdapSearchConstraints constraints)
 {
     LdapControl[] ldapControls =
     {
         new LdapSortControl(new LdapSortKey(pageInfo.OrderBy ?? LdapConfiguration.Attribute.UniqueName),
                             true),
         new LdapVirtualListControl(pageInfo.TotalResults, 0, pageInfo.TotalPerPage - 1, pageInfo.ContentCount)
     };
     constraints.setControls(ldapControls);
 }
Esempio n. 2
0
        private void ExecuteSearch(LdapFilter filter, LdapConnection ldapConnection, Action <LdapSearchResults> middleAction)
        {
            LdapSearchConstraints constraints = ldapConnection.SearchConstraints;

            SetLdapControls(filter, constraints);

            LdapSearchResults searchResults = ldapConnection.Search(
                filter.SearchPath, LdapConnection.SCOPE_SUB,
                filter.SearchCriteria, null, false, constraints);

            middleAction(searchResults);
            TotalResultsCallback(filter, searchResults);
        }
Esempio n. 3
0
        public IEnumerable <AdUser> GetAllUsers()
        {
            new Validator(this).ValidateConfiguration();
            string searchCriteria = $"(objectclass={LdapConfiguration.ObjectClass.User})";

            LdapFilter filter = new LdapFilter
            {
                SearchPath     = LdapConfiguration.Path,
                SearchCriteria = searchCriteria
            };
            IEnumerable <LdapEntry> results = SearchBy(filter);

            return(results.Select(found => new AdUser(found)));
        }
Esempio n. 4
0
        public IEnumerable <AdUser> GetUsersByFilter(string text, int page, out int total, int itemsPerPage = 5)
        {
            string searchCriteria =
                $"(&(objectClass={LdapConfiguration.ObjectClass.User})({LdapConfiguration.Attribute.UniqueName}=*{text}*))";

            LdapFilter filter = new LdapFilter
            {
                SearchPath     = LdapConfiguration.Path,
                SearchCriteria = searchCriteria,
                TotalPerPage   = itemsPerPage,
                CurrentPage    = page,
                SinglePage     = true
            };
            IEnumerable <LdapEntry> results = SearchBy(filter);

            total = filter.ContentCount;
            return(results.Select(found => new AdUser(found)));
        }
Esempio n. 5
0
 private static void TotalResultsCallback(LdapFilter pageInfo, LdapSearchResults searchResults)
 {
     LdapControl[] controls = searchResults.ResponseControls;
     if (controls == null)
     {
         pageInfo.ContentCount = pageInfo.TotalResults;
     }
     else
     {
         foreach (LdapControl control in controls)
         {
             if (control is LdapVirtualListResponse response)
             {
                 pageInfo.ContentCount = response.ContentCount;
             }
         }
     }
 }
Esempio n. 6
0
        public AdUser GetUserFromAdBy(string login)
        {
            new Validator(this).ValidateConfiguration();
            string searchCriteria =
                $"(&(objectClass={LdapConfiguration.ObjectClass.User})({LdapConfiguration.Attribute.UniqueName}={login}))";

            LdapFilter filter = new LdapFilter
            {
                SearchPath     = LdapConfiguration.Path,
                SearchCriteria = searchCriteria
            };
            LdapEntry userEntry = SearchBy(filter).FirstOrDefault();

            if (userEntry == null)
            {
                throw new Exception(
                          $"User {login} not found on {LdapConfiguration.Path} using ObjectClass {LdapConfiguration.ObjectClass.User} and uniqueName {LdapConfiguration.Attribute.UniqueName}");
            }

            return(new AdUser(userEntry, GetAdGroups(userEntry.GetDistinguishedName())));
        }
Esempio n. 7
0
        public IEnumerable <LdapEntry> SearchBy(LdapFilter filter)
        {
            List <LdapEntry> results = new List <LdapEntry>();

            using (LdapConnection ldapConnection = StartConnection())
            {
                if (filter.TotalResults > 1)
                {
                    LdapFilter countFilter = new LdapFilter
                    {
                        OrderBy        = filter.OrderBy,
                        TotalPerPage   = 1,
                        SearchCriteria = filter.SearchCriteria,
                        SearchPath     = filter.SearchPath
                    };
                    ExecuteSearch(countFilter, ldapConnection, (searchResults) => searchResults.Count());
                    filter.ContentCount = countFilter.ContentCount;
                }

                while (filter.ContentCount == -1 || filter.TotalResults < filter.ContentCount)
                {
                    ExecuteSearch(filter, ldapConnection, (searchResults) => ProcessResults(searchResults, results));

                    filter.TotalResults = results.Count;
                    if (filter.SinglePage)
                    {
                        break;
                    }
                }

                if (ldapConnection.Connected)
                {
                    ldapConnection.Disconnect();
                }
            }

            return(results);
        }
Esempio n. 8
0
        public IEnumerable <AdGroup> GetAdGroups(string loginWithPath = null)
        {
            new Validator(this).ValidateConfiguration();
            string searchCriteria = $"(objectClass={LdapConfiguration.ObjectClass.Group})";

            if (!string.IsNullOrWhiteSpace(loginWithPath))
            {
                searchCriteria = $"(&{searchCriteria}({LdapConfiguration.Attribute.GroupMember}={loginWithPath}))";
            }

            LdapFilter filter = new LdapFilter
            {
                SearchPath     = LdapConfiguration.Path,
                SearchCriteria = searchCriteria
            };

            IEnumerable <LdapEntry> results = SearchBy(filter);

            return(results.Select(found => new AdGroup
            {
                Code = found.GetUniqueName(),
                Name = found.GetDisplayName()
            }));
        }