public LdapUserConfig(string id, DbContext context)
        {
            ldapContext = new LdapContext(context);
            dirContext = new DivisionDirectoryContext();

            this.Member = dirContext.DirectoryMembers.GetByID(id);

            if (this.Member == null)
                throw new Exception(string.Format("No member with ID {0} was found in the directory", id));

            this.Context = context;

            this.LdapConfig = ldapContext.LdapConfigs.First();

            List<OuAssignment> ous = ldapContext.OuAssignments.Get(x => (x.MembershipScope & this.Member.MembershipScope) == x.MembershipScope).ToList();
            if (ous.Count == 0)
                throw new Exception("There are no OU's assigned to this user filter");

            if (ous.Count() > 1)
                throw new Exception("This member applies to multiple OU Assignments. Please consider refining your filter criteria");

            OuAssignment = ous.First();

            List<GroupAssignmentConfig> cfgs = ldapContext.GroupAssignmentConfigs.Get().ToList();
            this.GroupConfigs = new List<GroupAssignmentConfig>();

            foreach (GroupAssignmentConfig cfg in cfgs)
            {
                List<DirectoryMember> members = dirContext.DirectoryMembers.GetByFilter(cfg.MembershipScope, null);
                if (members.FirstOrDefault(x => x.InternalId == Member.InternalId) != null)
                    GroupConfigs.Add(cfg);
            }


            this.PersonalFolders = new List<PersonalFolder>();
            List<PersonalFolder> fldrs = ldapContext.PersonalFolders.Get().ToList();
            foreach (PersonalFolder f in fldrs)
            {
                List<DirectoryMember> members = dirContext.DirectoryMembers.GetByFilter(f.MembershipScope, null);
                if (members.FirstOrDefault(x => x.InternalId == Member.InternalId) != null)
                    PersonalFolders.Add(f);

            }

            string pwd = ldapContext.LdapConfigs.Decryptpassword(LdapConfig);
            PrincipalContext = new PrincipalContext(ContextType.Domain, LdapConfig.DomainPrincipalName, LdapConfig.UserName, pwd);

            ExistsInLdap = GetExistsInLdap();

            if (GetExistsInLdap())
                LdapUser = UserPrincipal.FindByIdentity(PrincipalContext, IdentityType.SamAccountName, Member.UserName);

        }
        public DirectoryMember AddToDirectory()
        {
            try
            {
                DivisionDirectoryContext context = new DivisionDirectoryContext();

                DirectoryMember m = new DirectoryMember(this);
                context.DirectoryMembers.Insert(m);
                return m;
            }
            catch(Exception ex)
            {
                throw new Exception("Could not add canidate tp directory", ex);
            }
        }