Exemplo n.º 1
0
        /// <summary>
        /// Creates a directory query from the <paramref name="connection"/>.
        /// </summary>
        /// <param name="connection">The connection to the directory.</param>
        /// <param name="namingContext">The location of the query.</param>
        /// <param name="scope">The scope of the query. Defaults to <see cref="SearchScope.Subtree"/>.</param>
        /// <param name="log">The log for query information. Defaults to null.</param>
        /// <param name="maxPageSize">The max page size for the directory. Defaults to 500.</param>
        /// <param name="withPaging">Indicates if paging should be used. Defaults to true.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="connection"/> is null or <paramref name="namingContext"/> is null, empty, or white space.</exception>
        /// <returns></returns>
        /// <example>
        /// using (var connection = new LdapConnection("localhost"))
        /// {
        ///     List{IDirectoryAttributes} users = connection.AsQueryable("DC=example,DC=com")
        ///         .Where(da => Filter.Equal(da, "", ""))
        /// }
        /// </example>
        public static IQueryable <IDirectoryAttributes> Query(this LdapConnection connection, string namingContext,
                                                              SearchScope scope = SearchScope.Subtree, ILinqToLdapLogger log = null, int maxPageSize = 500, bool withPaging = true)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }
            var provider = new DirectoryQueryProvider(connection, scope, new DynamicObjectMapping(namingContext, null, null), withPaging)
            {
                Log         = log,
                MaxPageSize = maxPageSize,
                IsDynamic   = true
            };
            var directoryQuery = new DirectoryQuery <IDirectoryAttributes>(provider);

            return(directoryQuery);
        }
Exemplo n.º 2
0
        /// <summary>
        /// List server information from RootDSE.
        /// </summary>
        /// <param name="connection">The connection to the directory.</param>
        /// <param name="log">The log for query information. Defaults to null.</param>
        /// <param name="attributes">
        /// Specify specific attributes to load.  Some LDAP servers require an explicit request for certain attributes.
        /// </param>
        /// <returns></returns>
        public static IDirectoryAttributes ListServerAttributes(this LdapConnection connection, string[] attributes = null, ILinqToLdapLogger log = null)
        {
            try
            {
                if (connection == null)
                {
                    throw new ArgumentNullException("connection");
                }
                using (var provider = new DirectoryQueryProvider(
                           connection, SearchScope.Base, new ServerObjectMapping(), false)
                {
                    Log = log, IsDynamic = true
                })
                {
                    var directoryQuery = new DirectoryQuery <IDirectoryAttributes>(provider);

                    var query = directoryQuery
                                .FilterWith("(objectClass=*)");

                    if (attributes != null && attributes.Length > 0)
                    {
                        query = query.Select(attributes);
                    }

                    var results = query.FirstOrDefault();

                    return(results ?? new DirectoryAttributes());
                }
            }
            catch (Exception ex)
            {
                if (log != null)
                {
                    log.Error(ex);
                }
                throw;
            }
        }