This class encapsulates an Active Directory connection, and handles common tasks
Пример #1
0
 /// <summary>
 /// Create a new AD User on the given connection
 /// </summary>
 /// <param name="connection">The AD connection</param>
 public ADUser(ActiveDirectory connection)
     : this(connection, new UserPrincipal(connection.Context))
 {
     _groups = new List<string>() { };
     _isNewUser = true;
     this.UserState = UserStates.New;
 }
        /// <summary>
        /// Create an AD User from the given connection and UserPrincipal
        /// </summary>
        /// <param name="connection">The AD connection</param>
        /// <param name="user">An existing UserPrincipal object</param>
        public ADUser(ActiveDirectory connection, UserPrincipal user)
        {
            if (connection == null || user == null)
            {
                throw new NullReferenceException();
            }

            _sourceUser = user;
            _connection = connection;
            mapper      = new DEMapper();
            DEFields.Add("manager");
            this.UserState = UserStates.Existing;

            foreach (System.Reflection.PropertyInfo property in this.GetType().GetProperties())
            {
                DEFieldAttribute tag = property.GetCustomAttributes(typeof(DEFieldAttribute), true).FirstOrDefault() as DEFieldAttribute;

                if (tag != null)
                {
                    DEFields.Add(tag.Name != null?tag.Name:property.Name);
                }
            }

            if (user.Name != null)
            {
                _groups = new List <string>(_sourceUser.GetGroups(connection.GlobalContext).Select(item => item.Name));
            }

            this.Initialize(_sourceUser);

            if (_sourceUser.DisplayName != null)
            {
                directoryEntry = _sourceUser.GetUnderlyingObject() as DirectoryEntry;
                directoryEntry.RefreshCache(DEFields.ToArray <string>());
                mapper.Copy(directoryEntry, this);
            }

            string ldap = String.Format("LDAP://{0}/{1}", connection.Name, connection.Container);

            parent = new DirectoryEntry(ldap, connection.User, connection.Password);
        }
Пример #3
0
        /// <summary>
        /// Create an AD User from the given connection and UserPrincipal
        /// </summary>
        /// <param name="connection">The AD connection</param>
        /// <param name="user">An existing UserPrincipal object</param>
        public ADUser(ActiveDirectory connection, UserPrincipal user)
        {
            if (connection == null || user == null)
            {
                throw new NullReferenceException();
            }

            _sourceUser = user;
            _connection = connection;
            mapper = new DEMapper();
            DEFields.Add("manager");
            this.UserState = UserStates.Existing;

            foreach (System.Reflection.PropertyInfo property in this.GetType().GetProperties())
            {
                DEFieldAttribute tag= property.GetCustomAttributes(typeof(DEFieldAttribute),true).FirstOrDefault() as DEFieldAttribute;

                if (tag != null)
                {
                    DEFields.Add(tag.Name!=null?tag.Name:property.Name);
                }
            }

            if (user.Name != null)
            {
                _groups = new List<string>(_sourceUser.GetGroups(connection.GlobalContext).Select(item => item.Name));
            }

            this.Initialize(_sourceUser);

            if (_sourceUser.DisplayName != null)
            {
                directoryEntry = _sourceUser.GetUnderlyingObject() as DirectoryEntry;
                directoryEntry.RefreshCache(DEFields.ToArray<string>());
                mapper.Copy(directoryEntry, this);
            }

            string ldap=String.Format("LDAP://{0}/{1}", connection.Name, connection.Container);
            parent = new DirectoryEntry(ldap, connection.User, connection.Password);
        }
 public ADGroup(ActiveDirectory connection, GroupPrincipal source)
 {
     _connection = connection;
     _source     = source;
 }
 /// <summary>
 /// Create an ADOBjectFactory for the given connection
 /// </summary>
 /// <param name="connection">An Active Directory connection</param>
 public ADObjectFactory(ActiveDirectory connection)
 {
     _connection = connection;
 }
Пример #6
0
        public main()
        {
            InitializeComponent();
            //Initialize bindings
            userBinding.DataSource = typeof(ADUser);
            groupBinding.DataSource = typeof(ICollection<String>);

            //Add bindings from controls to data structures
            userName.DataBindings.Add("Text", userBinding, "GivenName");
            userSurname.DataBindings.Add("Text", userBinding, "Surname");
            userDisplayName.DataBindings.Add("Text", userBinding, "DisplayName");
            userDescription.DataBindings.Add("Text", userBinding, "Description");
            userEmail.DataBindings.Add("Text", userBinding, "EmailAddress");
            userMainPhone.DataBindings.Add("Text", userBinding, "VoiceTelephoneNumber");
            userSAMAccountName.DataBindings.Add("Text", userBinding, "SamAccountName");
            userCompany.DataBindings.Add("Text", userBinding, "Company");
            userIPPhone.DataBindings.Add("Text", userBinding, "IPPhone");
            userMobile.DataBindings.Add("Text", userBinding, "Mobile");
            userPager.DataBindings.Add("Text", userBinding, "Pager");
            userPDON.DataBindings.Add("Text", userBinding, "PhysicalDeliveryOfficeName");
            userAddress.DataBindings.Add("Text", userBinding, "StreetAddress");
            userCity.DataBindings.Add("Text", userBinding, "City");
            userState.DataBindings.Add("Text", userBinding, "State");
            userPostalCode.DataBindings.Add("Text", userBinding, "PostalCode");
            userCountry.DataBindings.Add("Text", userBinding, "Country");
            userEmployeeType.DataBindings.Add("Text", userBinding, "EmployeeType");
            userTitle.DataBindings.Add("Text", userBinding, "Title");
            userDepartment.DataBindings.Add("Text", userBinding, "Department");
            userManager.DataBindings.Add("Text", userBinding, "Manager");
            userUPN.DataBindings.Add("Text", userBinding, "UserPrincipalName");

            userEnabled.DataBindings.Add("Checked", userBinding, "Enabled",true, DataSourceUpdateMode.OnPropertyChanged);
            searchResults.DataSource = userBinding;
            searchResults.AutoGenerateColumns = true;

            save.DataBindings.Add("Enabled", userBinding, "isSaveable");
            deleteButton.DataBindings.Add("Enabled", userBinding, "isDeleteable");

            //Handle the "position changed" event, so we can initialize non-bound controls for the new record
            userBinding.PositionChanged += userBindings_NewCurrent;

            userGroups.DataSource = groupBinding;
            groupBinding.DataSource = userBinding;
            groupBinding.DataMember = "Groups";
            userGroups.DisplayMember = "Groups";

            //Create a list of all our AD user controls so they can be iterated
            allUserControls = new List<Control>()
            {
                userName,userSurname, userDisplayName, userEmail, userMainPhone, userGroups,
                save, addGroup, removeGroup, deleteButton, userPassword1, userPassword2, userEnabled,
                userDescription, userSAMAccountName, newGroup
            };

            //Disable all controls by default
            set_Control_Enable(false);
            searchType.SelectedIndex=0;

            try
            {
                //Connect to the AD server
                ad = new ActiveDirectory(ConfigurationManager.AppSettings["AdContainer"],
                                            ConfigurationManager.AppSettings["AdName"],
                                            ConfigurationManager.AppSettings["AdUserId"],
                                            ConfigurationManager.AppSettings["AdPassword"]);
            }
            catch (PrincipalServerDownException ex)
            {
                //Print out the error text, write the status, and disable search controls
                Error_Message(ex.Message);
                Set_Status("AD connection failed");
                queryString.Enabled = false;
                search.Enabled = false;
                newButton.Enabled = false;
            }
        }
Пример #7
0
 public ADGroup(ActiveDirectory connection, GroupPrincipal source)
 {
     _connection = connection;
     _source = source;
 }
 /// <summary>
 /// Create an ADOBjectFactory for the given connection
 /// </summary>
 /// <param name="connection">An Active Directory connection</param>
 public ADObjectFactory(ActiveDirectory connection)
 {
     _connection = connection;
 }