Ejemplo n.º 1
0
        public override async Task <List <SearchResultReply> > OnSearchRequest(ClientContext context, ISearchEvent searchEvent)
        {
            SearchRequest searchRequest = new SearchRequest
            {
                RawPacket    = Google.Protobuf.ByteString.CopyFrom(searchEvent.SearchRequest.RawPacket),
                UserIdentity = ExtractUserIdentity(context.Rdn),
            };

            var response = await _ldapClient.ExecuteSearchRequestAsync(searchRequest);

            List <SearchResultReply> replies = new List <SearchResultReply>();

            foreach (var result in response.Results)
            {
                List <SearchResultReply.Attribute> attributes = new List <SearchResultReply.Attribute>();

                foreach (var attribute in result.Attributes)
                {
                    SearchResultReply.Attribute ldapAttribute = new SearchResultReply.Attribute(
                        attribute.Name,
                        attribute.Value.ToList()
                        );
                    attributes.Add(ldapAttribute);
                }

                SearchResultReply reply = new SearchResultReply(result.Rdn, attributes);
                replies.Add(reply);
            }

            return(replies);
        }
        public override Task <List <SearchResultReply> > OnSearchRequest(ClientContext context, ISearchEvent searchEvent)
        {
            System.Console.WriteLine(System.Convert.ToBase64String(searchEvent.SearchRequest.RawPacket));
            if (context.Rdn["cn"][0] == "OnlyBindUser")
            {
                return(Task.FromResult(new List <SearchResultReply>()));
            }

            int?limit = searchEvent.SearchRequest.SizeLimit;

            // Load the user database that queries will be executed against
            UserDatabase dbContainer = new UserDatabase();
            IQueryable <UserDatabase.User> userDb = dbContainer.GetUserDatabase().AsQueryable();

            var itemExpression = Expression.Parameter(typeof(UserDatabase.User));
            SearchExpressionBuilder searchExpressionBuilder = new SearchExpressionBuilder(searchEvent);
            var conditions  = searchExpressionBuilder.Build(searchEvent.SearchRequest.Filter, itemExpression);
            var queryLambda = Expression.Lambda <Func <UserDatabase.User, bool> >(conditions, itemExpression);
            var predicate   = queryLambda.Compile();

            var results = userDb.Where(predicate).ToList();

            List <SearchResultReply> replies = new List <SearchResultReply>();

            foreach (UserDatabase.User user in results)
            {
                List <SearchResultReply.Attribute> attributes = new List <SearchResultReply.Attribute>();
                SearchResultReply reply = new SearchResultReply(
                    user.Dn,
                    attributes
                    );

                foreach (KeyValuePair <string, List <string> > attribute in user.Attributes)
                {
                    SearchResultReply.Attribute attributeClass = new SearchResultReply.Attribute(attribute.Key, attribute.Value);
                    attributes.Add(attributeClass);
                }

                replies.Add(reply);
            }

            return(Task.FromResult(replies));
        }