private bool IsValidGroup(LdapConnection connection, LdapIdentity domain, string groupName, out LdapIdentity validatedGroup)
        {
            validatedGroup = null;

            var group        = LdapIdentity.ParseGroup(groupName);
            var searchFilter = $"(&(objectCategory=group)({group.TypeName}={group.Name}))";
            var response     = Query(connection, domain.Name, searchFilter, SearchScope.Subtree);

            for (var i = 0; i < response.Entries.Count; i++)
            {
                var entry  = response.Entries[i];
                var baseDn = LdapIdentity.BaseDn(entry.DistinguishedName);
                if (baseDn.Name == domain.Name) //only from user domain
                {
                    validatedGroup = new LdapIdentity
                    {
                        Name = entry.DistinguishedName,
                        Type = IdentityType.DistinguishedName
                    };

                    return(true);
                }
            }

            return(false);
        }
        private bool LoadProfile(LdapConnection connection, LdapIdentity domain, LdapIdentity user, out LdapProfile profile)
        {
            profile = null;

            var attributes   = new[] { "DistinguishedName", "displayName", "mail", "telephoneNumber", "mobile" };
            var searchFilter = $"(&(objectClass=user)({user.TypeName}={user.Name}))";

            var baseDn = SelectBestDomainToQuery(connection, user, domain);

            _logger.Debug($"Querying user '{user.Name}' in {baseDn.Name}");

            var response = Query(connection, baseDn.Name, searchFilter, SearchScope.Subtree, attributes);

            if (response.Entries.Count == 0)
            {
                _logger.Error($"Unable to find user '{user.Name}' in {baseDn.Name}");
                return(false);
            }

            var entry = response.Entries[0];

            profile = new LdapProfile
            {
                BaseDn            = LdapIdentity.BaseDn(entry.DistinguishedName),
                DistinguishedName = entry.DistinguishedName,
                DisplayName       = entry.Attributes["displayName"]?[0]?.ToString(),
                Email             = entry.Attributes["mail"]?[0]?.ToString(),
                Phone             = entry.Attributes["telephoneNumber"]?[0]?.ToString(),
                Mobile            = entry.Attributes["mobile"]?[0]?.ToString(),
            };

            _logger.Debug($"User '{user.Name}' profile loaded: {profile.DistinguishedName}");

            return(true);
        }